AmCharts序列图:轴点下方的中心对齐标签

时间:2018-06-18 06:21:45

标签: javascript charts amcharts

在此 AmCharts 序列图表(折线图)中,当渲染折线图时,类别轴中的标签从数据点交叉点右对齐。

我需要这些标签在数据点交叉比例下方居中对齐。

这是当前的源代码:

 chart = AmCharts.makeChart(id, {
        "type": "serial",
        "autoMarginOffset": 20,
        "usePrefixes":true,
        "prefixesOfBigNumbers":[
            {"number":1e+3,"prefix":"k"},
            {"number":1e+6,"prefix":"M"},
            {"number":1e+9,"prefix":"G"},
            {"number":1e+12,"prefix":"T"},
            {"number":1e+15,"prefix":"P"},
            {"number":1e+18,"prefix":"E"},
            {"number":1e+21,"prefix":"Z"},
            {"number":1e+24,"prefix":"Y"}
        ],
        "valueAxes": [{
            "id": "v1",
            "position": "left",
            "ignoreAxisWidth":false
        }],
        "balloon": {
            "borderThickness": 1,
            "shadowAlpha": 0,
        },
        "graphs": [{
            "id": "g1",
            "fillColors":color,
            "lineColor": color,
            "bullet": "round",
            "bulletBorderAlpha": 1,
            "bulletColor": "#FFFFFF",
            "fillColors": color,
            "bulletSize": 5,
            "hideBulletsCount": 50,
            "lineThickness": 2,
            "title": "red line",
            "useLineColorForBulletBorder": true,
            "valueField": "column-2"
        }],
        "chartCursor": {
            "valueLineEnabled": true,
            "valueLineBalloonEnabled": true,
            "cursorAlpha": 0,
            "valueLineAlpha": 0.5,
            "categoryBalloonDateFormat": 'JJ-NN'
        },
        "categoryField": "column-1",
        "categoryAxis": {
            "gridPosition": "start",
            "parseDates": true,
            "dashLength": 1,
            "minorGridEnabled": false,
            "minPeriod": "mm",
            "gridPosition":'middle',
            "centerLabels":true,
            "equalSpacing":false
        },
        "dataProvider": dataValue,
        "export": {
            "enabled": true
         },
         "allLabels": [{
            "text": timeperiod,
            "align": "center",
            "y":0
        }]
});

这是渲染图:

enter image description here

如何解决这个问题?

1 个答案:

答案 0 :(得分:0)

要使标签在解析日期时位于刻度的正下方,您必须将equalSpacing设置为true。

wrapper
var color = "#112233";
var timeperiod = "test";
var dataValue = generateData();
var chart = AmCharts.makeChart("chartdiv", {
        "type": "serial",
        "autoMarginOffset": 20,
        "usePrefixes":true,
        "prefixesOfBigNumbers":[
            {"number":1e+3,"prefix":"k"},
            {"number":1e+6,"prefix":"M"},
            {"number":1e+9,"prefix":"G"},
            {"number":1e+12,"prefix":"T"},
            {"number":1e+15,"prefix":"P"},
            {"number":1e+18,"prefix":"E"},
            {"number":1e+21,"prefix":"Z"},
            {"number":1e+24,"prefix":"Y"}
        ],
        "valueAxes": [{
            "id": "v1",
            "position": "left",
            "ignoreAxisWidth":false
        }],
        "balloon": {
            "borderThickness": 1,
            "shadowAlpha": 0,
        },
        "graphs": [{
            "id": "g1",
            "fillColors":color,
            "lineColor": color,
            "bullet": "round",
            "bulletBorderAlpha": 1,
            "bulletColor": "#FFFFFF",
            "fillColors": color,
            "bulletSize": 5,
            "hideBulletsCount": 50,
            "lineThickness": 2,
            "title": "red line",
            "useLineColorForBulletBorder": true,
            "valueField": "column-2"
        }],
        "chartCursor": {
            "valueLineEnabled": true,
            "valueLineBalloonEnabled": true,
            "cursorAlpha": 0,
            "valueLineAlpha": 0.5,
            "categoryBalloonDateFormat": 'JJ-NN'
        },
        "categoryField": "column-1",
        "categoryAxis": {
            //"gridPosition": "start", does not work with parseDates
            "parseDates": true,
            "dashLength": 1,
            "minorGridEnabled": false,
            "minPeriod": "mm",
            "gridPosition":'middle',
            //"centerLabelOnFullPeriod":false, //alternate solution but won't center directly under the axis tick
            "equalSpacing":true
        },
        "dataProvider": dataValue,
        "export": {
            "enabled": true
         },
         "allLabels": [{
            "text": timeperiod,
            "align": "center",
            "y":0
        }]
});

function generateData() {
  var data = [];
  var firstDate = new Date();
  firstDate.setHours(0,0,0,0);
  
  for (var i = 0; i < 10; ++i) {
    var newDate = new Date(firstDate);
    newDate.setMinutes(i);
    data.push({
      "column-1": newDate,
      "column-2": Math.floor(Math.random() * 20 + 1)
    });
  }
  
  return data;
}

如果您不想使用<script type="text/javascript" src="//www.amcharts.com/lib/3/amcharts.js"></script> <script type="text/javascript" src="//www.amcharts.com/lib/3/serial.js"></script> <div id="chartdiv" style="width: 100%; height: 300px;"></div>,也可以使用centerLabelOnFullPeriod,但是它只会将其稍微放在刻度线的右边,而不能放在中心。