0

我需要使用选择菜单更新多个 JSON 数组中的数据。

这个想法是这样的: 选择一个使用与该年份关联的数据更新全局变量(JSON 数组)的年份。

我的问题是我不确定变量范围以及如何控制它。我还发现 amcharts 会在加载时构建图表,因此更新变量不会做任何事情

是否可以使用新数据集更新全局变量,然后使用新数据重新加载图表?这会是什么样子?

不确定此更新是否有帮助,或者是否更清楚。

这是我构建的其中一个图表的小提琴链接:

http://jsfiddle.net/kenaesthetic/33JH9/

简写结构如下:

<head>
<script> amchart script </script>  
<head>

<body>

<select>
<option>2010</option
<option>2011</option
<option>2012</option
<option>2013</option
<option>2014</option
</select>

<div id="chart-container-1"></div> // These charts are stacked (refer to the fiddle)
<div id="chart-container-2"></div>
<div id="chart-container-3"></div>
<div id="chart-container-4"></div>
<div id="chart-container-5"></div>

</body>
4

1 回答 1

0

我想你想做更多这样的事情:

var chart;

var chartData = [
    {
        "subject": "Personal Services",
        "2010": 493142064,
        "2011": 540156996,
        "2012": 593541335,
        "2013": 640585618,
        "2014": 689417065
    }
];

AmCharts.ready(function () {
    // SERIALL CHART
    chart = new AmCharts.AmSerialChart();
    chart.dataProvider = chartData;
    chart.categoryField = "subject";
    chart.plotAreaBorderAlpha = 0.2;
    chart.rotate = true;

    // AXES
    // Category
    var categoryAxis = chart.categoryAxis;
    categoryAxis.gridAlpha = 0.1;
    categoryAxis.axisAlpha = 0;
    categoryAxis.gridPosition = "start";

    // value                      
    var valueAxis = new AmCharts.ValueAxis();
    valueAxis.stackType = "regular";
    valueAxis.gridAlpha = 0.1;
    valueAxis.axisAlpha = 0;
    chart.addValueAxis(valueAxis);

    // GRAPHS
    // firstgraph 
    var graph = new AmCharts.AmGraph();
    graph.title = "2010";
    graph.labelText = "[[value]]";
    graph.valueField = "2010";
    graph.type = "column";
    graph.lineAlpha = 0;
    graph.fillAlphas = 1;
    graph.lineColor = "#C72C95";
    graph.balloonText = "<b><span style='color:#C72C95'>[[title]]</b></span><br><span style='font-size:14px'>[[category]]: <b>[[value]]</b></span>";
    chart.addGraph(graph);

    // second graph                              
    graph = new AmCharts.AmGraph();
    graph.title = "2011";
    graph.labelText = "[[value]]";
    graph.valueField = "2011";
    graph.type = "column";
    graph.lineAlpha = 0;
    graph.fillAlphas = 1;
    graph.lineColor = "#D8E0BD";
    graph.balloonText = "<b><span style='color:#afbb86'>[[title]]</b></span><br><span style='font-size:14px'>[[category]]: <b>[[value]]</b></span>";
    chart.addGraph(graph);

    // third graph              
    graph = new AmCharts.AmGraph();
    graph.title = "2012";
    graph.labelText = "[[value]]";
    graph.valueField = "2012";
    graph.type = "column";
    graph.lineAlpha = 0;
    graph.fillAlphas = 1;
    graph.lineColor = "#B3DBD4";
    graph.balloonText = "<b><span style='color:#74bdb0'>[[title]]</b></span><br><span style='font-size:14px'>[[category]]: <b>[[value]]</b></span>";
    chart.addGraph(graph);

    // fourth graph 
    graph = new AmCharts.AmGraph();
    graph.title = "2013";
    graph.labelText = "[[value]]";
    graph.valueField = "2013";
    graph.type = "column";
    graph.lineAlpha = 0;
    graph.fillAlphas = 1;
    graph.lineColor = "#69A55C";
    graph.balloonText = "<b><span style='color:#69A55C'>[[title]]</b></span><br><span style='font-size:14px'>[[category]]: <b>[[value]]</b></span>";
    chart.addGraph(graph);

    // fifth graph 
    graph = new AmCharts.AmGraph();
    graph.title = "2014";
    graph.labelText = "[[value]]";
    graph.valueField = "2014";
    graph.type = "column";
    graph.lineAlpha = 0;
    graph.fillAlphas = 1;
    graph.lineColor = "#B5B8D3";
    graph.balloonText = "<b><span style='color:#7a81be'>[[title]]</b></span><br><span style='font-size:14px'>[[category]]: <b>[[value]]</b></span>";
    chart.addGraph(graph);

    // LEGEND
    var legend = new AmCharts.AmLegend();
    legend.position = "right";
    legend.borderAlpha = 0.3;
    legend.horizontalGap = 10;
    legend.switchType = "v";
    chart.addLegend(legend);

    // WRITE
    chart.write("chartdiv");
});

// Make chart 2D/3D
function setDepth() {
    if (document.getElementById("rb1").checked) {
        chart.depth3D = 0;
        chart.angle = 0;
    } else {
        chart.depth3D = 20;
        chart.angle = 30;
    }
    chart.validateNow();
}

此示例基于 AmChart 的堆叠图表示例代码。它确实将图表的数据分离成一个全局 Javascript 变量,即 chartData。

您需要将问题分解为两部分:数据的操作,然后在数据更改后将数据加载到图表中。

这是使用 jQuery 和按钮操作数据的非常简单的示例。http://jsfiddle.net/stevewilhelm/XzmQ6/2/

修改数据后,您将需要更新图表。以 http://blog.amcharts.com/2013/08/tutorial-live-editing-chart-data.html为例。

并查看 chart.dataProvider chart.validateData() 了解详情。

于 2013-11-02T05:24:46.967 回答