图表上的数据事件绑定

时间:2015-05-11 00:52:43

标签: javascript kendo-ui kendo-chart

默认情况下,图表显示2001 - 2002年的数据;我想知道当用户将鼠标悬停在图表上时,它将显示2002-2003的数据。一旦mouser离开图表,它就应该回到默认阶段。

//The first data comes from 2001-2002
var seriesData = [{
    year: "2000",
    sold: 100,
    produced: 200
}, {
    year: "2001",
    sold: 250,
    produced: 280
}];

// The second dataset comes from 2002-2003
var seriesData2 = [{
    year: "2002",
    sold: 140,
    produced: 240
}, {
    year: "2004",
    sold: 350,
    produced: 380
}];

function createChart() {
    $("#chart").kendoChart({
    dataSource: {
        data: seriesData
    },
    series: [{
        name: "Sold",
        field: "sold"
    }, {
        name: "Producted",
        field: "produced"
    }],
    categoryAxis: {
        field: "year"
    },
  });
}

$(document)
    .ready(createChart);

这是jsfiddle:https://jsfiddle.net/epvg86qu/1/

1 个答案:

答案 0 :(得分:5)

将图表包裹在一个容器中,例如div,然后在该div上放置一个mouseover和out事件。然后更改图表的数据源

<div id="chart-container">
//your chart here
</div>

<script type="text/javascript">
var isHover = false;
$("#chart-container").hover(
function () {
    if (!isHover) {
        var chart = $("#chart").data().kendoChart;
        chart.dataSource.data(seriesData2);
        isHover = true;
    }
}, function () {
    if (isHover) {
        var chart = $("#chart").data().kendoChart;
        chart.dataSource.data(seriesData);
        isHover = false;
    }
});
</script>