【发布时间】:2017-01-26 04:21:51
【问题描述】:
我希望将字符串用作折线图的 Google Chart api 的 X 轴标签,我在其中加入两组数据以在单个折线图中绘制两条线,我设法使其在没有字符串标签的情况下工作(与数字一起使用)但没有显示轴线
google.charts.load('current', {'packages': ['corechart']});
google.charts.setOnLoadCallback(drawChart);
function drawChart() {
var data1 = new google.visualization.DataTable();
data1.addColumn('string', 'Month');
data1.addColumn('number', 'Average Sales Price');
data1.addColumn({type: 'string', role: 'tooltip', p: {'html': true}});
data1.addRows([
[{v: 0, f:'Q4 2013'}, 3,'<span class="total-home-tooltip">$300K Some html - 1!</span>'],
[{v: 1, f:'Q1 2014'}, 6,'<span class="total-home-tooltip">$600K Some html - 2!</span>'],
[{v: 2, f:'Q2 2014'}, 5,'<span class="total-home-tooltip">$500K Some html - 3!</span>'],
[{v: 3, f:'Q3 2014'}, 8,'<span class="total-home-tooltip">$800K Some html - 4!</span>'],
[{v: 4, f:'Q4 2014'}, 2,'<span class="total-home-tooltip">$200K Some html - 5!</span>'],
[{v: 5, f:'Q1 2015'}, 5,'<span class="total-home-tooltip">$500K Some html - 6!</span>'],
[{v: 6, f:'Q2 2015'}, 5,'<span class="total-home-tooltip">$500K Some html - 7!</span>']
]);
var data2 = new google.visualization.DataTable();
data2.addColumn('string', 'Month');
data2.addColumn('number', 'Median Sales Price');
data2.addColumn({type: 'string', role: 'tooltip', p: {'html': true}});
/*if we would like to show string in x asix we have to use data in format {v: 0, f:'Q4 2013'} otherwise google api will not draw haxis lines */
data2.addRows([
[{v: 0, f:'Q4 2013'}, 5,'<span class="total-home-tooltip">Some html here</span>'],
[{v: 1, f:'Q1 2014'}, 1,'<span class="total-home-tooltip">Some html here</span>'],
[{v: 2, f:'Q2 2014'}, 3,'<span class="total-home-tooltip">Some html here</span>'],
[{v: 3, f:'Q3 2014'}, 9,'<span class="total-home-tooltip">Some html here</span>'],
[{v: 4, f:'Q4 2014'}, 4,'<span class="total-home-tooltip">Some html here</span>'],
[{v: 5, f:'Q1 2015'}, 5,'<span class="total-home-tooltip">Some html here</span>'],
[{v: 6, f:'Q2 2015'}, 7,'<span class="total-home-tooltip">Some html here</span>']
]);
var options1 = {
legend: {
position: 'bottom',
textStyle: {color: 'black', fontSize: 14},
pointShape: "triangle",
alignment: 'end'
},
interpolateNulls: true,
height: 312,
width: 1070,
colors: ['#EBAD00','#00ACE0'],
pointSize: 8,
backgroundColor: {fill: "transparent"},
tooltip: {isHtml: true},
hAxis: {
minValue: 0,
baselineColor:"#BBBBBB"
},
vAxis: {
gridlines: {color: "transparent"},
minValue: 0,
baselineColor:"#BBBBBB"
},
ticks: [{v: 0, f:'Jan'},{v: 1, f:'Feb'},{v: 2, f:'Mar'},{v: 3, f:'Apr'},{v: 4, f:'May'},{v: 5, f:'June'},{v: 6, f:'July'}], // <------- This does the trick
annotations: {
style: 'line'
}
};
var joinedData = google.visualization.data.join(data1, data2, 'full', null, [1,2], [1,2]);
var chart = new google.visualization.LineChart(document.getElementById('market_trend_chart1'));
chart.draw(joinedData, options1 );
}
<div id="market_trend_chart1"></div>
我收到此错误错误:类型不匹配。值 0 与列索引 0 中的类型字符串不匹配,我知道错误是变量类型不匹配,但不太确定如何解决此问题
没有字符串标签的工作小提琴http://jsfiddle.net/j29Pt/536/(问题是现在没有haxis线) 如果我尝试将字符串添加为标签http://jsfiddle.net/j29Pt/535/,图表似乎停止工作
【问题讨论】:
标签: javascript charts google-visualization