当其中之一突出显示时如何更改所有列的颜色

时间:2019-01-15 04:02:07

标签: amcharts

所以我想更改除选定的列以外的所有列的颜色。我该怎么做呢?

var chart = AmCharts.makeChart( "chartdiv", {
 "type": "serial",
 "theme": "none",
 "dataProvider": [ {
   "country": "USA",
   "visits": 2025
 }, {
   "country": "China",
   "visits": 1882
 }, {
   "country": "Japan",
   "visits": 1809
 } ],
 "valueAxes": [ {
   "gridColor": "#FFFFFF",
   "gridAlpha": 0.2,
   "dashLength": 0
 } ],
 "gridAboveGraphs": true,
 "startDuration": 1,
 "graphs": [ {
   "balloonText": "[[category]]: <b>[[value]]</b>",
   "fillAlphas": 0.8,
   "lineAlpha": 0.2,
   "fillColors": changeColour,
   "type": "column",
   "valueField": "visits"
 } ],
 "chartCursor": {
   "categoryBalloonEnabled": false,
   "cursorAlpha": 0,
   "zoomable": false
 },
 "categoryField": "country",
 "categoryAxis": {
   "gridPosition": "start",
   "gridAlpha": 0,
   "tickPosition": "start",
   "tickLength": 20
 },
 "export": {
   "enabled": true
 }

  } );

我创建了一个函数,如果值在一定数值范围内,我将尝试返回该颜色,但这不起作用。

1 个答案:

答案 0 :(得分:1)

在AmCharts 3中,没有一种方法可以通过fillColors中的函数动态更改条形的颜色,因为它仅接受一个字符串(AmCharts 4具有适配器,通过该适配器可以执行此操作)

您可以在v3中实现此功能的几个选择:

1)使用fillColorsField并用所需的颜色标记数据中的项目:

 "dataProvider": [ {
   "country": "USA",
   "visits": 2025
 }, {
   "country": "China",
   "visits": 1882
 }, {
   "country": "Japan",
   "visits": 1809,
   "fillColor": "#ff0000"
 } ],
 // ...
 "graphs": [ {
   "balloonText": "[[category]]: <b>[[value]]</b>",
   "fillAlphas": 0.8,
   "lineAlpha": 0.2,
   "fillColorsField": "fillColor", //match the property in your data 
   "type": "column",
   "valueField": "visits"
 } ],

下面的演示

var chart = AmCharts.makeChart("chartdiv", {
  "type": "serial",
  "theme": "none",
  "dataProvider": [{
    "country": "USA",
    "visits": 2025
  }, {
    "country": "China",
    "visits": 1882
  }, {
    "country": "Japan",
    "fillColor": "#ff0000", //make this one red
    "visits": 1809
  }],
  "valueAxes": [{
    "gridColor": "#FFFFFF",
    "gridAlpha": 0.2,
    "dashLength": 0
  }],
  "gridAboveGraphs": true,
  "startDuration": 1,
  "graphs": [{
    "balloonText": "[[category]]: <b>[[value]]</b>",
    "fillAlphas": 0.8,
    "lineAlpha": 0.2,
    "fillColorsField": "fillColor",
    "type": "column",
    "valueField": "visits"
  }],
  "chartCursor": {
    "categoryBalloonEnabled": false,
    "cursorAlpha": 0,
    "zoomable": false
  },
  "categoryField": "country",
  "categoryAxis": {
    "gridPosition": "start",
    "gridAlpha": 0,
    "tickPosition": "start",
    "tickLength": 20
  },
  "export": {
    "enabled": true
  }

});
html,
body {
  width: 100%;
  height: 100%;
  margin: 0px;
}

#chartdiv {
  width: 100%;
  height: 100%;
}
<link href="//www.amcharts.com/lib/3/plugins/export/export.css" media="all" rel="stylesheet" type="text/css" />
<script src="//www.amcharts.com/lib/3/amcharts.js"></script>
<script src="//www.amcharts.com/lib/3/serial.js"></script>
<script src="//www.amcharts.com/lib/3/themes/light.js"></script>
<script src="//www.amcharts.com/lib/3/plugins/export/export.min.js"></script>
<div id="chartdiv"></div>

2)如果列值低于negativeFillColors,请使用negativeFillColorsnegativeBase使图表自动将颜色更改为negativeBase中的颜色:

 "graphs": [ {
   "balloonText": "[[category]]: <b>[[value]]</b>",
   "fillAlphas": 0.8,
   "lineAlpha": 0.2,
   "negativeBase": 1810, //columns with values less than this will be filled red
   "negativeFillColors": "#ff0000",
   "type": "column",
   "valueField": "visits"
 } ],

下面的演示

var chart = AmCharts.makeChart("chartdiv", {
  "type": "serial",
  "theme": "none",
  "dataProvider": [{
    "country": "USA",
    "visits": 2025
  }, {
    "country": "China",
    "visits": 1882
  }, {
    "country": "Japan",
    "visits": 1809
  }],
  "valueAxes": [{
    "gridColor": "#FFFFFF",
    "gridAlpha": 0.2,
    "dashLength": 0
  }],
  "gridAboveGraphs": true,
  "startDuration": 1,
  "graphs": [{
    "balloonText": "[[category]]: <b>[[value]]</b>",
    "fillAlphas": 0.8,
    "lineAlpha": 0.2,
    "negativeBase": 1810,
    "negativeFillColors": "#ff0000",
    "type": "column",
    "valueField": "visits"
  }],
  "chartCursor": {
    "categoryBalloonEnabled": false,
    "cursorAlpha": 0,
    "zoomable": false
  },
  "categoryField": "country",
  "categoryAxis": {
    "gridPosition": "start",
    "gridAlpha": 0,
    "tickPosition": "start",
    "tickLength": 20
  },
  "export": {
    "enabled": true
  }

});
html,
body {
  width: 100%;
  height: 100%;
  margin: 0px;
}

#chartdiv {
  width: 100%;
  height: 100%;
}
<link href="//www.amcharts.com/lib/3/plugins/export/export.css" media="all" rel="stylesheet" type="text/css" />
<script src="//www.amcharts.com/lib/3/amcharts.js"></script>
<script src="//www.amcharts.com/lib/3/serial.js"></script>
<script src="//www.amcharts.com/lib/3/themes/light.js"></script>
<script src="//www.amcharts.com/lib/3/plugins/export/export.min.js"></script>
<div id="chartdiv"></div>