Amcharts 3D图表-类别轴与值轴错误混合

时间:2018-11-08 14:46:10

标签: javascript charts amcharts

我正在使用3D的amcharts(即depth3D = 40),图表显示良好,但类别轴与值轴重叠/合并

我正在使用类似这样的东西:

var chart = AmCharts.makeChart("chartdiv", {
  "type": "serial",
  "theme": "light",
  "startDuration": 1,   
  "valueAxes": [{
    "id": "v1",
    "title": "Cost (in USD)",
    "position": "left",
    "autoGridCount": false,
    "labelFunction": function(value) {
      return "$" + Math.round(value);
    }
  }, {
    "id": "v2",
    "title": "Effort ( hh:mm )",
    "gridAlpha": 0,
    "position": "right",
    "autoGridCount": false,
    "duration": "mm",
    "durationUnits": {
      "hh": "h ",
      "mm": "min"
     }
  }],

  ---------------------
  ---------------------

 "depth3D": 40,
 "angle": 10,    
  "chartCursor": {
    "pan": true,
    "cursorPosition": "mouse",
    "cursorAlpha": 0.1,
    "cursorColor": "#000000",
    "fullWidth": true,
    "valueLineAlpha": 0.2
  },
  "categoryField": "lob",
   "categoryAxis": {
        "gridPosition": "start",
        "axisAlpha":0.9,
        "axisThickness": 1,                     
        "axisColor": "black",        
        "gridAlpha":0,
        "labelRotation":25,
        "fontSize":10,
        "boldLabels":true

    },

这是我的 fiddle

这是值轴(红色)与类别轴合并/混合。

任何避免轴合并/重叠的建议将不胜感激!

1 个答案:

答案 0 :(得分:1)

使用angle / depth3D启用3D后,图表透视图将移动。您看到的红线是值轴线的一部分,由于新的透视图而延伸到3D空间中,因此这是设计使然;请注意,如果您沿相反的方向设置角度,则另一根轴也会这样做。结果,没有办法使用配置删除它。作为一种技巧,您可以尝试通过drawn事件直接编辑SVG,方法是查询axis line by class/id设置为true的addClassNames并修改d属性:

function remove3DSegment() {
  var line = document.querySelector('.value-axis-v2 .amcharts-axis-line');
  var lineSVG = line.getAttribute('d');
  line.setAttribute('d', lineSVG.substr(0, lineSVG.lastIndexOf(' '))); //last part of the SVG path command contains the 3D segment on the bottom
}

var chart = AmCharts.makeChart("chartdiv", {
  "type": "serial",
  "theme": "light",
  "startDuration": 1,
  "addClassNames": true,
  // ...
  "listeners": [{
    "event": "drawn",
    "method": remove3DSegment
  }],
  // ...
});

如果要对其进行概括,可以让该函数通过事件查找图表的ID和值轴的ID。

function remove3DSegment(e) {
  var divId = e.chart.div.id;
  var valueAxisId = e.chart.valueAxes[1].id; //assumes your second axis is the one that needs to be modified.
  var line = document.querySelector('#' + divId + ' .value-axis-' + valueAxisId + ' .amcharts-axis-line');
  var lineSVG = line.getAttribute('d');
  line.setAttribute('d', lineSVG.substr(0, lineSVG.lastIndexOf(' '))); //last part of the SVG path command contains the 3D segment on the bottom
}

请注意,这是一个技巧,如果在以后的v3版本中更改了用于绘制轴的SVG路径命令,则可能会中断。

function remove3DSegment() {
  var line = document.querySelector('.value-axis-v2 .amcharts-axis-line');
  var lineSVG = line.getAttribute('d');
  line.setAttribute('d', lineSVG.substr(0, lineSVG.lastIndexOf(' ')));
}
var chart = AmCharts.makeChart("chartdiv", {
  "type": "serial",
  "theme": "light",
  "startDuration": 1,
  "addClassNames": true,
  "valueAxes": [{
    "id": "v1",
    "title": "Cost (in USD)",
    "position": "left",
    "autoGridCount": false,
    "labelFunction": function(value) {
      return "$" + Math.round(value);
    }
  }, {
    "id": "v2",
    "title": "Effort ( hh:mm )",
    "gridAlpha": 0,
    "position": "right",
    "axisAlpha": 0.9,
    "axisThickness": 2,
    "axisColor": "red",
  }],
  "graphs": [{
    "id": "g4",
    "valueAxis": "v1",
    "lineColor": "#3B7610",
    "fillColors": "#3B7610",
    "fillAlphas": 1,
    "type": "column",
    "title": "Cost saving per year",
    "valueField": "costSaving",
    "clustered": false,
    "columnWidth": 0.3,
    "topRadius": 1,
    // "legendValueText": "$[[value]]M",
    "balloonText": "[[title]]<br /><b style='font-size: 90%'>$[[value]]M</b>"
  }, {
    "id": "g1",
    "valueAxis": "v2",
    "bullet": "round",
    "bulletBorderAlpha": 1,
    "bulletColor": "#FFFFFF",
    "bulletSize": 5,
    "hideBulletsCount": 50,
    "lineThickness": 2,
    "lineColor": "#20acd4",
    "type": "smoothedLine",
    "title": "Effort saving per year",
    "topRadius": 0.95,
    "useLineColorForBulletBorder": true,
    "valueField": "effortSaving",
    "balloonText": "[[title]]<br /><b style='font-size: 90%'>[[value]]</b>"
  }],
  "depth3D": 40,
  "angle": 10,
  "chartCursor": {
    "pan": true,
    "cursorPosition": "mouse",
    "cursorAlpha": 0.1,
    "cursorColor": "#000000",
    "fullWidth": true,
    "valueLineAlpha": 0.2
  },
  "categoryField": "lob",
  "categoryAxis": {
    "gridPosition": "start",
    "axisAlpha": 0.9,
    "axisThickness": 1,
    "axisColor": "black",
    "gridAlpha": 0,
    "labelRotation": 25,
    "fontSize": 10,
    "boldLabels": true

  },
  "legend": {
    "horizontalGap": 5,
    "maxColumns": 30,
    "useGraphSettings": true,
    "markerSize": 10,
    "leftMargin": 0,
    "valueText": ""
  },
  "balloon": {
    "borderThickness": 1,
    "shadowAlpha": 0
  },
  "export": {
    "enabled": true
  },
  "listeners": [{
    "event": "drawn",
    "method": remove3DSegment
  }],
  "dataProvider": [{
    "lob": "abca",
    "effortSaving": 64140,
    "costSaving": 3600
  }, {
    "lob": "dfasdf",
    "effortSaving": 326724,
    "costSaving": 1875
  }, {
    "lob": "dfgsdfgt",
    "effortSaving": 36864,
    "costSaving": 1500,
  }, {
    "lob": "gfsdg",
    "effortSaving": 101808,
    "costSaving": 3614,
  }, {
    "lob": "fgfgf",
    "effortSaving": 13200,
    "costSaving": 6215,
  }, {
    "lob": "jytujty",
    "effortSaving": 111312,
    "costSaving": 3123,
  }, {
    "lob": "erqwr",
    "effortSaving": 5040,
    "costSaving": 1235,
  }]
});
#chartdiv {
  width: 100%;
  height: 500px;
}
<script src="https://www.amcharts.com/lib/3/amcharts.js"></script>
<script src="https://www.amcharts.com/lib/3/serial.js"></script>
<script src="https://www.amcharts.com/lib/3/plugins/export/export.min.js"></script>
<link rel="stylesheet" href="https://www.amcharts.com/lib/3/plugins/export/export.css" type="text/css" media="all" />
<script src="https://www.amcharts.com/lib/3/pie.js"></script>
<script src="https://www.amcharts.com/lib/3/themes/light.js"></script>
<div id="chartdiv"></div>