Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
414 views
in Technique[技术] by (71.8m points)

amcharts4 - Color column graph by groups in Amcharts

I have a graph with columns for which I would like to color bars according to the value of a third variable (Correction). The color for “Yes” has to be #FF4B35 and for “No”: #1C7BFF.

I also need to show the legend on the top of my graph.

Here, is the graph I would like to have: enter image description here

Here is my code:

// Create chart instance
var chart = am4core.create("chartdiv", am4charts.XYChart);

// Add data
chart.data = [
  {
    "Pays": "Croatie",
    "Co?t par hab.": 4.32,
    "Correction": "Oui"
  },
  {
    "Pays": "Bulgarie",
    "Co?t par hab.": 1.728,
    "Correction": "Oui"
  },
  {
    "Pays": "Grèce",
    "Co?t par hab.": 5.168,
    "Correction": "No"
  },
  {
    "Pays": "Roumanie",
    "Co?t par hab.": 4.672,
    "Correction": "Oui"
  },
  {
    "Pays": "Lettonie",
    "Co?t par hab.": 7.264,
    "Correction": "No"
  }];

// Create axis X
var categoryAxis = chart.xAxes.push(new am4charts.CategoryAxis());
categoryAxis.dataFields.category = "Pays";
categoryAxis.renderer.grid.template.location = 0;
categoryAxis.renderer.minGridDistance = 1;
categoryAxis.renderer.labels.template.rotation = -45;
categoryAxis.renderer.labels.template.verticalCenter = "top";
categoryAxis.renderer.labels.template.horizontalCenter = "right";
categoryAxis.renderer.grid.template.disabled = true;

// Create axis Y
var valueAxis = chart.yAxes.push(new am4charts.ValueAxis());
valueAxis.title.text = "Euros PPA par habitant";



// Create series for column
var series1_1 = chart.series.push(new am4charts.ColumnSeries());
series1_1.dataFields.valueY = "Co?t par hab.";
series1_1.dataFields.categoryX = "Pays";
series1_1.strokeWidth = 0;
series1_1.fillOpacity = 0.7;

Does anyone know how to do it?

question from:https://stackoverflow.com/questions/66067218/color-column-graph-by-groups-in-amcharts

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...