我有一个包含3条线的动画折线图。
提琴:
https://jsfiddle.net/t5rxamd7/
我正在按月显示数据,如2020年12月,2020年1月等。如何显示每个月的多个值。目前我的对象是这样的
var chart_object = {"Dec 19": {monthLabel: "Dec", date: "30-Dec-2019", total: "42", cats: "0", dogs: "55", catspercentage: "0", dogsspercentage: "131"}, "Jan 20": {monthLabel: "Jan 2020", date: "", total: "", cats: "", dogs: ""}, "Feb 20": {monthLabel: "Feb", date: "", total: "", cats: "", dogs: ""}, "Mar 20": {monthLabel: "Mar", date: "", total: "", cats: "", dogs: ""}};
对于每个月,我不想显示总计,而是希望每天显示它而不更改x轴。 也就是说,图表线应显示每日进度,但x轴仍应显示相同的标签。
我该怎么做?谁能帮助我做到这一点?
谢谢。
答案 0 :(得分:1)
首先,只需要打包-> 'corechart'
-'bar'
和'line'
用于绘制材料图表
将x轴列更改为日期类型...
var chartData = new google.visualization.DataTable();
chartData.addColumn('date', 'Date'); <-- change to date
添加hAxis
选项format: 'MMM yy'
删除hAxis
和minValue
的{{1}}选项
如果要替换,它们将是日期,而不是数字(viewWindow
)
0
将hAxis: {
format: 'MMM yy', // <-- add
//minValue: 0, // <-- remove
viewWindowMode: "explicit",
//viewWindow: { min: 0 }, // <-- remove
用作数据行中的第一个值,此处...
new Date(date)
最后,当在x轴上使用日期(采用月格式)时,
x轴标签将重复。
将需要构建自定义rawData.push([ new Date(date), total, {v: catscount, f: catspercentageAnnotation}, {v: dogscount, f: dogsspercentageAnnotation}]);
在这里,我们使用ticks
来确定要添加多少getColumnRange
ticks
请参阅以下工作片段...
// build x-axis ticks to prevent repeated labels
var dateRange = chartData.getColumnRange(0);
var ticks = [];
var dateTick = dateRange.min;
while (dateTick.getTime() <= dateRange.max.getTime()) {
ticks.push(dateTick);
dateTick = new Date(dateTick.getFullYear(), dateTick.getMonth() + 2, 0);
}
options.hAxis.ticks = ticks;
google.charts.load('current', {
packages: ['corechart']
});
google.setOnLoadCallback(prepareChartData);
function prepareChartData(){
var chartData = new google.visualization.DataTable();
chartData.addColumn('date', 'Date');
chartData.addColumn('number', 'Total');
chartData.addColumn('number', 'Dogs');
chartData.addColumn('number', 'Cats');
title = 'My Chart';
var options = {
title: title,
curveType: 'function',
legend: {position: 'bottom', alignment: 'start'},
colors: ['#003f5c', '#ffa600', '#665191', '#f95d6a'],
annotations: {
alwaysOutside: true,
textStyle: {
color: 'black',
fontSize: 11
},
},
hAxis: {
format: 'MMM yy',
viewWindowMode: "explicit",
},
vAxis: {
minValue: 0,
viewWindowMode: "explicit",
viewWindow: { min: 0 },
title: ''
},
titleTextStyle: {
color:'#3a3a3a',
fontSize:24,
bold:false
// fontName: "Segoe UI"
},
bar: {groupWidth: '95%'},
bars: 'horizontal'
};
var chartDivId = "chart_div";
var chart = new google.visualization.LineChart(document.getElementById(chartDivId));
var rawData =[];
var chart_object = {"Dec 19": {monthLabel: "Dec", date: "30-Dec-2019", total: "42", cats: "0", dogs: "55", catspercentage: "0", dogsspercentage: "131"}, "Jan 20": {monthLabel: "Jan 2020", date: "31-Jan-2020", total: "0", cats: "0", dogs: "0"}, "Feb 20": {monthLabel: "Feb", date: "29-Feb-2020", total: "0", cats: "0", dogs: "0"}, "Mar 20": {monthLabel: "Mar", date: "31-Mar-2020", total: "0", cats: "0", dogs: "0"}};
$.each(chart_object, function(i, chartobject) {
date = chartobject.date;
total = parseInt(chartobject.total);
catscount = parseInt(chartobject.cats);
dogscount = parseInt(chartobject.dogs);
catspercentage = parseInt(chartobject.catspercentage);
catspercentageAnnotation = catscount+", percent "+catspercentage+"%";
dogsspercentage = parseInt(chartobject.dogsspercentage);
dogsspercentageAnnotation = dogscount+", percent "+dogsspercentage+"%";
rawData.push([ new Date(date), total, {v: catscount, f: catspercentageAnnotation}, {v: dogscount, f: dogsspercentageAnnotation}]);
});
var counter = 0;
drawChart();
function drawChart() {
if(counter < rawData.length){
chartData.addRow(rawData[counter]);
// build x-axis ticks to prevent repeated labels
var dateRange = chartData.getColumnRange(0);
var ticks = [];
var dateTick = dateRange.min;
while (dateTick.getTime() <= dateRange.max.getTime()) {
ticks.push(dateTick);
dateTick = new Date(dateTick.getFullYear(), dateTick.getMonth() + 2, 0);
}
options.hAxis.ticks = ticks;
chart.draw(chartData, options);
counter++;
window.setTimeout(drawChart, 1000);
}
}
}