Dojo图表选择性标签为x轴和&格

时间:2012-08-03 04:39:12

标签: javascript charts dojo axis-labels

我需要创建一个道场图表

http://codebins.com/bin/4ldqp8z/1

在上面的链接中,我的图表有x轴标签,如

当前x轴: 11:am,12:40 pm,2:20 pm&下午4:00

对于相同的标签,我有网格线。

现在要求是我只需要3个标签

所需的x轴:上午10点,下午1点&下午4:00

图表显示下午9:30至下午4点之间的数据,间隔为10分钟。

这是工作样本: - http://codebins.com/bin/4ldqp8z/1

这里是代码: - 你需要在http

上运行它
<html>

    <head>
        <meta content="text/html; charset=UTF-8" http-equiv="content-type">
        <title>Code Bins</title>
                            <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/dojo/1.7.2/dojo/dojo.js" ></script>


    </head>

    <body Body Params>
        <div id="chart1" style="width:300px;height:200px">
</div>    </body>
                        <script type="text/javascript">
                    require(["dojo/_base/declare", "dojo/dom-style", "dojo/ready", "dojox/charting/Chart",  "dojox/charting/action2d/PlotAction", "dojox/charting/axis2d/Default", "dojox/charting/plot2d/Columns", "dojox/charting/plot2d/Lines", "dojox/charting/plot2d/Grid"], function(declare, domStyle, ready, Chart, PlotAction, Default, Columns, Lines, Grid) {

    ready(function() {
        var chart = new Chart("chart1")
            .addAxis("y", { vertical: true, 
                fixLower: "major", 
                fixUpper: "major", 
                leftBottom: false,
                majorTicks:false,
                minorTicks:false,
                stroke: {color:"#FFF",width:0},
                majorTick:{length:0}})
            .addAxis("x", {
                labels: labels, /* Lables are coming from here*/
                majorTicks:false,
                majorTick:{length:4,color:"#FFFFFF"},
                majorLabels:true,
                majorTickStep:10,
                minorTicks:false,
                max:40,
                stroke: {color:"#CFCFCF",width:"1"}})
            .addSeries("y", close, {  fill: "#EBEBEB"})
            .addPlot("Grid", {
                    type: "Grid",
                    markers: true
                });

        chart.render();
    });
});

var close = [];
for (var i = 0; i < 40; i++) { close.push(parseInt((Math.random() * 100) + 1200)); }
var labels = [{value: 1,text: "9:30am"},
{value: 2,text: "9:40am"},{value: 3,text: "9:50am"},{value: 4,text: "10am"},{value: 5,text: "10:10am"},{value: 6,text: "10:20am"},{value: 7,text: "10:30am"},{value: 8,text: "10:40am"},{value: 9,text: "10:50am"},{value: 10,text: "11am"},{value: 11,text: "11:10am"},{value: 12,text: "11:20am"},{value: 13,text: "11:30am"},{value: 14,text: "11:40am"},{value: 15,text: "11:50am"},{value: 16,text: "12pm"},{value: 17,text: "12:10pm"},{value: 18,text: "12:20pm"},{value: 19,text: "12:30pm"},{value: 20,text: "12:40pm"},{value: 21,text: "12:50pm"},{value: 22,text: "1pm"},{value: 23,text: "1:10pm"},{value: 24,text: "1:20pm"},{value: 25,text: "1:30pm"},{value: 26,text: "1:40pm"},{value: 27,text: "1:50pm"},{value: 28,text: "2pm"},{value: 29,text: "2:10pm"},{value: 30,text: "2:20pm"},{value: 31,text: "2:30pm"},{value: 32,text: "2:40pm"},{value: 33,text: "2:50pm"},{value: 34,text: "3pm"},{value: 35,text: "3:10pm"},{value: 36,text: "3:20pm"},{value: 37,text: "3:30pm"},{value: 38,text: "3:40pm"},{value: 39,text: "3:50pm"},{value: 40,text: "4pm"}];
// BOTTOM OF PAGE
                </script>



</html>

2 个答案:

答案 0 :(得分:1)

您的X标签明确将主要刻度设置为10。根据你的标签判断它应该是18:1是10分钟,6是1小时,18是3小时你想要的。所以这部分很简单。问题是步骤从0开始计数,0是9:20 am。你应该从一个不同的数字开始。一种可行的方法是将上午10点变为零。这意味着您应该将数据和标签移回,例如:

var labels = [
  {value: -3,text: "9:30am"},
  {value: -2,text: "9:40am"},
  {value: -1,text: "9:50am"},
  {value:  0,text: "10am"},
  {value:  1,text: "10:10am"},
  {value:  2,text: "10:20am"}
  // and so on
];

或者从早上7点开始,就像那样:

var labels = [
  // we have no data for 7am, so we skip labels too
  {value: 15,text: "9:30am"},
  {value: 16,text: "9:40am"},
  {value: 17,text: "9:50am"},
  {value: 18,text: "10am"},
  {value: 19,text: "10:10am"},
  {value: 20,text: "10:20am"}
  // and so on
];

尝试以不同的方式映射您的序数,您可以达到预期的效果。

PS:与控制小蜱和微蜱的方式相同。

答案 1 :(得分:0)

找到解决方案http://codebins.com/bin/4ldqp8z/2

扩展axis2d和它的一些如此简单。不需要计算任何事情的步骤。只要给出你想要的任何标签。

dojo.provide("myDojo.myAxis");
dojo.declare("myDojo.myAxis", dojox.charting.axis2d.Default, {
        calculate: function(min, max, span, labels){
            this.inherited(arguments);
            this.ticks.major= [{ value:4, label:"10am"},{ value:22, label:"1pm"},{ value:40, label:"4pm"}]
            return this;
        }
    });
      dojox.charting.axis2d.myAxis = myDojo.myAxis;

添加轴时添加类型

.addAxis("x", {
                labels: labels, /* Lables are coming from here*/
                majorTicks:false,
                majorTick:{length:4,color:"#FFFFFF"},
                majorLabels:true,
                majorTickStep:10,
                minorTicks:false,
                max:40,
                type:"myAxis",
                stroke: {color:"#CFCFCF",width:"1"}})