Return x+轴的值 - Amchart
Return value for x+axis - Amchart
我有一个问题要问你。我想编写一个函数 return 将我发送到图表的 x 轴值。
例如,当我点击图表中的 "Aug 24" 时,我的函数 return "Aug 24".
有什么想法吗?
您可以为此使用 categoryAxis
"clickItem" 事件。即:
"categoryAxis": {
"parseDates": true,
"listeners": [{
"event": "clickItem",
"method": function(item) {
alert(item.value);
}
}]
}
这是一个完整的工作示例:
var chartData = generateChartData();
var chart = AmCharts.makeChart( "chartdiv", {
"type": "serial",
"dataProvider": chartData,
"graphs": [ {
"lineColor": "#FCD202",
"bullet": "square",
"bulletBorderThickness": 1,
"hideBulletsCount": 30,
"title": "yellow line",
"valueField": "value1",
"fillAlphas": 0
}, {
"valueAxis": "v3",
"lineColor": "#B0DE09",
"bullet": "triangleUp",
"bulletBorderThickness": 1,
"hideBulletsCount": 30,
"title": "green line",
"valueField": "value2",
"fillAlphas": 0
} ],
"chartScrollbar": {},
"chartCursor": {
"cursorPosition": "mouse"
},
"categoryField": "date",
"categoryAxis": {
"parseDates": true,
"listeners": [{
"event": "clickItem",
"method": function(item) {
alert(item.value);
}
}]
}
} );
// generate some random data, quite different range
function generateChartData() {
var chartData = [];
var firstDate = new Date();
firstDate.setDate( firstDate.getDate() - 20 );
for ( var i = 0; i < 20; i++ ) {
// we create date objects here. In your data, you can have date strings
// and then set format of your dates using chart.dataDateFormat property,
// however when possible, use date objects, as this will speed up chart rendering.
var newDate = new Date( firstDate );
newDate.setDate( newDate.getDate() + i );
var value1 = Math.round( Math.random() * 80 ) + 500;
var value2 = Math.round( Math.random() * 80 ) + 500;
chartData.push( {
date: newDate,
value1: value1,
value2: value2
} );
}
return chartData;
}
<script src="http://www.amcharts.com/lib/3/amcharts.js"></script>
<script src="http://www.amcharts.com/lib/3/serial.js"></script>
<div id="chartdiv" style="width: 100%; height: 200px;"></div>
我有一个问题要问你。我想编写一个函数 return 将我发送到图表的 x 轴值。
例如,当我点击图表中的 "Aug 24" 时,我的函数 return "Aug 24".
有什么想法吗?
您可以为此使用 categoryAxis
"clickItem" 事件。即:
"categoryAxis": {
"parseDates": true,
"listeners": [{
"event": "clickItem",
"method": function(item) {
alert(item.value);
}
}]
}
这是一个完整的工作示例:
var chartData = generateChartData();
var chart = AmCharts.makeChart( "chartdiv", {
"type": "serial",
"dataProvider": chartData,
"graphs": [ {
"lineColor": "#FCD202",
"bullet": "square",
"bulletBorderThickness": 1,
"hideBulletsCount": 30,
"title": "yellow line",
"valueField": "value1",
"fillAlphas": 0
}, {
"valueAxis": "v3",
"lineColor": "#B0DE09",
"bullet": "triangleUp",
"bulletBorderThickness": 1,
"hideBulletsCount": 30,
"title": "green line",
"valueField": "value2",
"fillAlphas": 0
} ],
"chartScrollbar": {},
"chartCursor": {
"cursorPosition": "mouse"
},
"categoryField": "date",
"categoryAxis": {
"parseDates": true,
"listeners": [{
"event": "clickItem",
"method": function(item) {
alert(item.value);
}
}]
}
} );
// generate some random data, quite different range
function generateChartData() {
var chartData = [];
var firstDate = new Date();
firstDate.setDate( firstDate.getDate() - 20 );
for ( var i = 0; i < 20; i++ ) {
// we create date objects here. In your data, you can have date strings
// and then set format of your dates using chart.dataDateFormat property,
// however when possible, use date objects, as this will speed up chart rendering.
var newDate = new Date( firstDate );
newDate.setDate( newDate.getDate() + i );
var value1 = Math.round( Math.random() * 80 ) + 500;
var value2 = Math.round( Math.random() * 80 ) + 500;
chartData.push( {
date: newDate,
value1: value1,
value2: value2
} );
}
return chartData;
}
<script src="http://www.amcharts.com/lib/3/amcharts.js"></script>
<script src="http://www.amcharts.com/lib/3/serial.js"></script>
<div id="chartdiv" style="width: 100%; height: 200px;"></div>