我怎样才能访问dojox图表?

时间:2012-10-10 09:28:22

标签: javascript charts dojo

如何访问dojox图表并将其作为js-object获取? 在普通的dijit小部件中,它与registry.byId("id")一起使用,但这不适用于dojox图表。

我也尝试过dom.byId,但我需要它作为调用函数的对象,而不是dom节点。

这是我的代码:

    <!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="dijit/themes/claro/claro.css">
<script>dojoConfig = {async: true}</script>
<script src="//ajax.googleapis.com/ajax/libs/dojo/1.8.0/dojo/dojo.js"></script>
<script>
require(["dojox/charting/Chart", "dojox/charting/axis2d/Default", "dojox/charting/plot2d/Lines", "dojox/charting/plot2d/Grid", "dojox/charting/themes/Claro", "dojo/ready"],
  function(Chart, Default, Lines, Grid, theme, ready){
  ready(function(){
    var chart1 = new Chart("chartId");
    chart1.setTheme(theme);
    chart1.addPlot("default", {type: Lines});
    chart1.addPlot("Grid", {type: "Grid"});
    chart1.addAxis("x",{}); 
    chart1.addAxis("y", {vertical: true});
    chart1.addSeries("Visits",[
        { x: 1, y: 200 },
        { x: 2, y: 185 },
        { x: 3, y: 100 },
        { x: 4, y: 110 },
        { x: 5, y: 130 },
        { x: 6, y: 70 },
        { x: 7, y: 30 },
        { x: 8, y: 200 }
    ]);

    chart1.render();
  });
});
require(["dojo/ready", "dijit/form/Button", "dojo/dom"], function(ready, Button, dom){
    ready(function(){
        var button = new Button({
            label: "Change my label"
        }, "buttonId");
    });
});

function tryToAccess()
{
    require(["dojo/ready", "dijit/registry", "dojo/dom"], function(ready, registry, dom){
        ready(function(){
            var button = registry.byId("buttonId");
            console.log(button); /*returns an object*/
            button.set("label","successfully changed!");

            /*var chart = ????????????????; */
        });
    });
}
</script>
</head>
<body class="claro">
<button id="buttonId"></button>
<div id="chartId" style="height: 300px;"></div>

<!-- JUST 4 DEBUGGING -->
<button onClick="tryToAccess();">try to access</button>
</body>
</html>

1 个答案:

答案 0 :(得分:0)

dojox.charting.Chart不是小部件,请尝试使用dojox.charting.widget.Chart。我认为registry.byId可以解决这个问题。

var chart1_widget = new Chart({}, "chartId");
var chart1 = chart1_widget.chart;
chart1.setTheme(theme);
...