道场烛台图表 - 用json更新系列

时间:2012-04-23 13:33:42

标签: charts dojo

我正在使用Dojo工具包来创建烛台图表。我每年都有许多不同的数据系列,用户可以点击按钮选择数据年份,烛台图表将会更新。当我直接在代码中获取数据时,我已经成功地完成了这项工作:

makeCharts = function() {

  series2012 =  [
        {open: 477, close: 471.5, high: 477,  low:  471.5,  mid:  474.2,  tooltip:"Orange  <br />Rate: 474.2"},
        {open: 524.5, close: 442.1, high: 524.5,  low: 442.1,  mid:  481.7,  tooltip:"Blue <br />Rate: 481.7"},etc.......
  series2011 =  [
        {open: 600, close: 481.5, high: 600,  low:  481.5,  mid:  490.2,  tooltip:"Orange  <br />Rate: 490.2"},
        {open: 554.5, close: 412.1, high: 554.5,  low: 412.1,  mid:  481.7,  tooltip:"Blue <br />Rate: 481.7"},etc.......

添加系列:

chart1.addSeries("Series 1", series2012,{stroke: {color:"#003399"}, fill: "#3399cc"}); 

然后点击按钮更新系列:

<button onclick="chart1.updateSeries('Series 1', series2011);chart1.render();">2011</button>

我真正想做的是从外部json文件中查询数据。添加系列时我能够这样做,但不能更新它。 (请参阅html中的问号 - 这是我不确定要输入的内容):

代码:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<link rel="stylesheet" type="text/css"    href="https://serverapi.arcgisonline.com/jsapi/arcgis/2.5/js/dojo/dijit/themes/soria/soria.css" />
<style type="text/css">
    body, html { font-family:helvetica,arial,sans-serif; font-size:90%; }
    #charts {clear: both;margin-bottom: 50px;}
    .chart-area {float: left;border: 1px solid #ccc;width:  450px;height: 350px;margin: 3px;}
    .chart {width:  450px;height: 300px;}
</style>
<script type="text/javascript"   src="http://ajax.googleapis.com/ajax/libs/dojo/1.7.2/dojo/dojo.js" djConfig="parseOnLoad: true"></script>
<script type="text/javascript">
dojo.require("dojo.data.ItemFileReadStore");
dojo.require("dijit.form.Button");        
dojo.require("dojox.charting.action2d.Tooltip"); 
dojo.require("dojox.charting.plot2d.Candlesticks");
dojo.require("dojox.charting.Chart2D");
dojo.require("dojox.charting.DataSeries");
dojo.require("dojox.charting.action2d.Shake");

dojo.addOnLoad(function() {
     store4 = new dojo.data.ItemFileReadStore({
     url: "candle_update1.json"});

makeCharts = function(){ 
  chart1 = new dojox.charting.Chart2D("candleChart");
  chart1.addPlot("default", {type: "Candlesticks", gap: 2, minBarSize: 5, maxBarSize: 10});
  chart1.addAxis("x", {              
    natural: true,          
    majorLabels: true,      
    includeZero: false, 
    majorTickStep: 1,
    majorTick: {
    stroke: "black",
    length: 5
  },
  fixUpper:"major",
  stroke: "black",

  labels: [          
    {value: 1,  text: "Orange"},
    {value: 2,  text: "Red"},
    {value: 3,  text: "Blue"},
    {value: 4,  text: "Black"},
    {value: 5,  text: "Yellow"},
    {value: 6,  text: "White"},
    {value: 7,  text: "Green"},
    {value: 8,  text: "Purple"},
    {value: 9,  text: "Brown"}
  ],rotation:-45         
  });

    chart1.addAxis("y", {   
      vertical: true,
      leftBottom: true,
      includeZero: true,
      majorLabels: true,
      fixUpper: "major",
      fixed: true,
      max: 800
    });

 chart1.addSeries("Series 1", new dojox.charting.DataSeries(
  store4, {query: {series: 2012}}, {open: "open", close: "close", high: "high", low: "low", mid: "mid", tooltip: "tooltip"}));

    var anim2 = new dojox.charting.action2d.Tooltip(chart1, "default");
    var anim1a = new dojox.charting.action2d.Shake(chart1, "default");
};
dojo.addOnLoad(function(){
    makeCharts();
});
});   
</script>
</head>
<body class="soria">
    <h1>Dojo Candlesticks update test</h1>
    <div id="charts">
      <div class="chart-area">
        <div id="candleChart" class="chart"></div>
      </div>
    </div>
    <span style="font-size:14px;"><strong>Select years:</strong></span>
    <span id="btn">
      <button onclick="chart1.updateSeries('Series 1', ?????????);chart1.render();">2012</button> 
      <button onclick="chart1.updateSeries('series1', ????????);chart1.render();">2011</button> 
    </span>
</body>
</html>

外部json文件(candle_update1.json):

  

{ “标识符”: “ID”,
  “标签”: “县”,
  “项目”:   { “县志”: “橙”, “ID”: “1”, “系列”:2012年, “开放”:477, “闭”:471.5, “高”:477, “低”:471.5, “MID” :474.2, “提示”: “橙色
率:474.2”},   { “县志”: “蓝”, “ID”: “2”, “系列”:2012年, “开放”:524.5, “闭”:442.1, “高”:524.5, “低”:442.1, “MID” :481.7, “提示”: “蓝
率:481.7”},   { “县志”: “红色”, “ID”: “3”, “系列”:2012年, “开放”:508.1, “闭”:481, “高”:508.1, “低”:481, “中间” :494.4, “提示”: “红
率:494.4”},   { “县志”: “黑”, “ID”: “4”, “系列”:2012年, “开放”:503.2, “闭”:439.3, “高”:503.2, “低”:439.3, “MID” :470.4, “提示”: “黑
率:470.4”},   { “县志”: “黄”, “ID”: “5”, “系列”:2012年, “开放”:492.1, “闭”:430.1, “高”:492.1, “低”:430.1, “MID” :460.3, “提示”: “黄
率:460.3”},   { “县志”: “白”, “ID”: “6”, “系列”:2012年, “开放”:495.5, “闭”:425.5, “高”:495.5, “低”:425.5, “MID” :459.6, “提示”: “白
率:459.6”},   { “县志”: “绿色”, “ID”: “7”, “系列”:2012年, “开放”:550.9, “闭”:414.4, “高”:550.9, “低”:414.4, “MID” :478.2, “提示”: “绿色
率:478.2”},   { “县志”: “紫”, “ID”: “8”, “系列”:2012年, “开放”:484.8, “闭”:431.5, “高”:484.8, “低”:431.5, “MID” :457.6, “提示”: “紫
率:457.6”},   { “县志”: “布朗”, “ID”: “9”, “系列”:2012年, “开放”:486.6, “闭”:419.6, “高”:486.6, “低”:419.6, “MID” :452.1, “提示”: “布朗
率:452.1”},   { “县志”: “橙”, “ID”: “1A”, “系列”:2011年, “开放”:600, “闭”:481.5, “高”:600, “低”:481.5, “MID” :490.2, “提示”: “橙色
率:450.2”},   { “县志”: “蓝”, “ID”: “2A”, “系列”:2011年, “开放”:554.5, “闭”:412.1, “高”:554.5, “低”:412.1, “MID” :451.7, “提示”: “蓝
率:451.7”},   { “县志”: “红色”, “ID”: “3A”, “系列”:2011年, “开放”:608.1, “闭”:481, “高”:608.1, “低”:481, “中间” :594.4, “提示”: “红
率:474.4”},   { “县志”: “黑”, “ID”: “4A”, “系列”:2011年, “开放”:703.2, “闭”:409.3, “高”:703.2, “低”:409.3, “MID” :470.4, “提示”: “黑
率:470.4”},   { “县志”: “黄”, “ID”: “5A”, “系列”:2011年, “开放”:392.1, “闭”:230.1, “高”:392.1, “低”:230.1, “MID” :260.3, “提示”: “黄
率:260.3”},   { “县志”: “白”, “ID”: “6A”, “系列”:2011年, “开放”:495.5, “闭”:425.5, “高”:495.5, “低”:425.5, “MID” :459.6, “提示”: “白
率:459.6”},   { “县志”: “绿色”, “ID”: “7A”, “系列”:2011年, “开放”:750.9, “闭”:414.4, “高”:750.9, “低”:414.4, “MID” :478.2, “提示”: “绿色
率:478.2”},   { “县志”: “紫”, “ID”: “8A”, “系列”:2011年, “开放”:584.8, “闭”:431.5, “高”:584.8, “低”:431.5, “MID” :457.6, “提示”: “紫
率:457.6”},   { “县志”: “布朗”, “ID”: “9A”, “系列”:2011年, “开放”:786.6, “闭”:319.6, “高”:786.6, “低”:319.6, “MID” :452.1, “工具提示”: “布朗
率:452.1”}

]}

所以,我一直在努力如何更新系列。如何格式化或编写更新系列的数据数组部分以获取正确的数据系列?

谢谢,非常感谢任何帮助!

1 个答案:

答案 0 :(得分:0)

你最好的选择是使用商店和Observable对象。这样,存储更新时数据会更新。

// Initial data
var data = [
    // This information, presumably, would come from a database or web service
    // Note that the values for site are either 1 or 2
    { id: 1, value: 20, site: 1 },
    { id: 2, value: 16, site: 1 },
    { id: 3, value: 11, site: 1 },
    { id: 4, value: 18, site: 1 },
    { id: 5, value: 26, site: 1 },
    { id: 6, value: 19, site: 2 },
    { id: 7, value: 20, site: 2 },
    { id: 8, value: 28, site: 2 },
    { id: 9, value: 12, site: 2 },
    { id: 10, value: 4, site: 2 }
];

// Create the data store
// Store information in a data store on the client side
var store = dojo.store.Observable(new dojo.store.Memory({
    data: {
        identifier: "id",
        label: "Users Online",
        items: data
    }
}));

您可以通过XHR从外部文件加载商店

这就是你如何获得系列剧

// Adds a StoreSeries to the y axis, queries for all site 1 items
chart.addSeries("y", new dojox.charting.StoreSeries(store, { query: { site: 1 } }, "value"));