google.charts.load('current', {'packages': ['line']});
google.charts.setOnLoadCallback(drawCurveTypes);
function drawCurveTypes() {
var data = new google.visualization.DataTable();
data.addColumn('number', 'values');
data.addColumn('number', 'a');
data.addColumn('number', 'b');
data.addColumn('number', 'c');
data.addColumn('number', 'd');
data.addColumn('number', 'e');
data.addColumn('number', 'f');
data.addColumn('number', 'g');
data.addRows([[3,4,5,10,11,12,13,12], [6,7,8,10,11,12,13,12],[9,10,11,10,11,12,13,12]]);
var options = {
chart: {
title: "Example"
}
};
var line=document.getElementById('chart_div');
var chart = new google.charts.Line(line);
chart.draw(data,google.charts.Line.convertOptions(options));
}
<script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script>
<div id="chart_div"></div>
这是我的图表。它是一个实质性折线图(包:google.charts.Line,而不是google.visualization.LineChart)。如您所见,“ SPPlus”已截止。我试图调整图表的高度,但这并没有解决我的问题。
答案 0 :(得分:2)
您可以增加图表的高度。
在提供的代码段中可能不起作用的原因,
选项在错误的位置使用。
在代码段中,选项已添加到图表的构造函数中。 因此,没有使用任何提供的选项,包括标题。
var chart = new google.charts.Line(line,google.charts.Line.convertOptions(options));
chart.draw(data);
相反,应将它们添加到draw方法中。
var chart = new google.charts.Line(line);
chart.draw(data, google.charts.Line.convertOptions(options));
您还可以减小字体大小。
参见以下工作片段...
google.charts.load('current', {'packages': ['line']});
google.charts.setOnLoadCallback(drawCurveTypes);
function drawCurveTypes() {
var data = new google.visualization.DataTable();
data.addColumn('number', 'values');
data.addColumn('number', 'a');
data.addColumn('number', 'b');
data.addColumn('number', 'c');
data.addColumn('number', 'd');
data.addColumn('number', 'e');
data.addColumn('number', 'f');
data.addColumn('number', 'g');
data.addRows([[3,4,5,10,11,12,13,12], [6,7,8,10,11,12,13,12],[9,10,11,10,11,12,13,12]]);
var options = {
chart: {
title: 'Example'
},
height: 400,
legend: {
textStyle: {
fontSize: 10
}
}
};
var line = document.getElementById('chart_div');
var chart = new google.charts.Line(line);
chart.draw(data, google.charts.Line.convertOptions(options));
}
<script src="https://www.gstatic.com/charts/loader.js"></script>
<div id="chart_div"></div>
另一种选择是绘制自己的自定义图例,
在图表上方,这将提供更多空间,
不增加高度。
参见以下工作片段...
google.charts.load('current', {'packages': ['line']});
google.charts.setOnLoadCallback(drawCurveTypes);
function drawCurveTypes() {
var data = new google.visualization.DataTable();
data.addColumn('number', 'values');
data.addColumn('number', 'a');
data.addColumn('number', 'b');
data.addColumn('number', 'c');
data.addColumn('number', 'd');
data.addColumn('number', 'e');
data.addColumn('number', 'f');
data.addColumn('number', 'g');
data.addRows([[3,4,5,10,11,12,13,12], [6,7,8,10,11,12,13,12],[9,10,11,10,11,12,13,12]]);
var options = {
chart: {
title: 'Example'
},
legend: {
position: 'none'
}
};
var line = document.getElementById('chart_div');
var chart = new google.charts.Line(line);
// add legend marker
function addLegendMarker(markerProps) {
var legendMarker = document.getElementById('template-legend-marker').innerHTML;
for (var handle in markerProps) {
if (markerProps.hasOwnProperty(handle)) {
legendMarker = legendMarker.replace('{{' + handle + '}}', markerProps[handle]);
}
}
document.getElementById('legend_div').insertAdjacentHTML('beforeEnd', legendMarker);
}
// chart ready event
google.visualization.events.addListener(chart, 'ready', function () {
var legend = document.getElementById('legend_div');
// get colors from chart
var colorPallette = [];
var lines = line.getElementsByTagName('path');
Array.prototype.forEach.call(lines, function(path) {
if (colorPallette.indexOf(path.getAttribute('stroke')) === -1) {
colorPallette.push(path.getAttribute('stroke'));
}
});
// clear previous legend
legend.innerHTML = '';
// add legend marker for each Y axis column - skip X axis --> i = 1
for (var i = 1; i < data.getNumberOfColumns(); i++) {
var markerProps = {};
markerProps.index = i;
markerProps.color = colorPallette[i - 1];
markerProps.label = data.getColumnLabel(i);
addLegendMarker(markerProps);
}
// add "hover" event to each legend marker
var currentSelection;
var markers = legend.getElementsByTagName('DIV');
Array.prototype.forEach.call(markers, function(marker) {
marker.addEventListener('mouseover', function (e) {
currentSelection = chart.getSelection();
var marker = e.target || e.srcElement;
marker = marker.closest('.legend-marker');
var columnIndex = parseInt(marker.getAttribute('data-columnIndex'));
chart.setSelection([{column: columnIndex}]);
}, false);
marker.addEventListener('mouseout', function (e) {
chart.setSelection([]);
}, false);
});
});
chart.draw(data, google.charts.Line.convertOptions(options));
}
#legend_div {
color: #999;
font-family: Roboto;
position: absolute;
right: 0px;
text-align: right;
top: 0px;
width: 60%;
z-index: 1000;
}
.legend-marker {
display: inline-block;
padding: 6px 6px 6px 6px;
}
.legend-marker-color {
border-radius: 25%;
display: inline-block;
height: 12px;
width: 12px;
}
<script src="https://www.gstatic.com/charts/loader.js"></script>
<div id="chart_div"></div>
<div id="legend_div"></div>
<!-- template for building marker -->
<script id="template-legend-marker" type="text/html">
<div class="legend-marker" data-columnIndex="{{index}}">
<div class="legend-marker-color" style="background-color: {{color}}"></div>
<span>{{label}}</span>
</div>
</script>
答案 1 :(得分:0)
我创建了一个包含12列的图表,它没有中断,而是显示了一个切换按钮以移至下一列。
我认为您已经弄乱了配置本身。
HTML
<script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script>
<div id="chart_div"></div>
JS
google.charts.load('current', {packages: ['corechart', 'line']});
google.charts.setOnLoadCallback(drawCurveTypes);
function parseData(input){
return input.reduce(function(o,i){
o.push(i.concat(i).concat(i).concat(i).concat(i));
return o;
},[]);
}
function drawCurveTypes() {
var data = new google.visualization.DataTable();
data.addColumn('number', 'X');
data.addColumn('number', 'a');
data.addColumn('number', 'b');
data.addColumn('number', 'c');
data.addColumn('number', 'd');
data.addColumn('number', 'e');
data.addColumn('number', 'f');
data.addColumn('number', 'g');
data.addColumn('number', 'h');
data.addColumn('number', 'i');
data.addColumn('number', 'j');
data.addColumn('number', 'j');
data.addColumn('number', 'k');
data.addColumn('number', 'l');
data.addColumn('number', 'm');
data.addRows(parseData([
[0, 0, 0], [1, 10, 5], [2, 23, 15], [3, 17, 9], [4, 18, 10], [5, 9, 5],
[6, 11, 3], [7, 27, 19], [8, 33, 25], [9, 40, 32], [10, 32, 24], [11, 35, 27],
[12, 30, 22], [13, 40, 32], [14, 42, 34], [15, 47, 39], [16, 44, 36], [17, 48, 40],
[18, 52, 44], [19, 54, 46], [20, 42, 34], [21, 55, 47], [22, 56, 48], [23, 57, 49],
[24, 60, 52], [25, 50, 42], [26, 52, 44], [27, 51, 43], [28, 49, 41], [29, 53, 45],
[30, 55, 47], [31, 60, 52], [32, 61, 53], [33, 59, 51], [34, 62, 54], [35, 65, 57],
[36, 62, 54], [37, 58, 50], [38, 55, 47], [39, 61, 53], [40, 64, 56], [41, 65, 57],
[42, 63, 55], [43, 66, 58], [44, 67, 59], [45, 69, 61], [46, 69, 61], [47, 70, 62],
[48, 72, 64], [49, 68, 60], [50, 66, 58], [51, 65, 57], [52, 67, 59], [53, 70, 62],
[54, 71, 63], [55, 72, 64], [56, 73, 65], [57, 75, 67], [58, 70, 62], [59, 68, 60],
[60, 64, 56], [61, 60, 52], [62, 65, 57], [63, 67, 59], [64, 68, 60], [65, 69, 61],
[66, 70, 62], [67, 72, 64], [68, 75, 67], [69, 80, 72]
]));
var options = {
height: 500,
hAxis: {
title: 'Time'
},
vAxis: {
title: 'Popularity'
},
series: {
1: {curveType: 'function'}
}
};
var chart = new google.visualization.LineChart(document.getElementById('chart_div'));
chart.draw(data, options);
}
JSFiddle演示-https://jsfiddle.net/z0wudpxv/1/