Multiple amCharts 4 in firefox - amcharts

I try to load multiple amcharts charts on one page. The charts work perfectly with both Chrome and Edge. Firefox shows only the first graph and not the other graphs. Hopefully someone can show me how I can solve this problem.
previously question does not have a visible solution
In this example I have made 2 amcharts graphs. If I open this example in Chrome or Edge then all graphs works but not in Firefox. Hopefully with the help of this example you can find out what the problem is.
// Themes begin
am4core.useTheme(am4themes_animated);
// Themes end
var gantt = am4core.create("gantt", am4charts.XYChart);
gantt.hiddenState.properties.opacity = 0; // this creates initial fade-in
gantt.paddingRight = 30;
gantt.dateFormatter.inputDateFormat = "dd.MM.yyyy";
var colorSet = new am4core.ColorSet();
colorSet.saturation = 0.4;
gantt.data = [
{
category: 'First',
fromDate: '01.01.2022',
toDate: '01.03.2022',
fromDateReal: '01.02.2022',
toDateReal: '01.04.2022',
colorPlan: colorSet.getIndex(0).brighten(0),
colorReal: colorSet.getIndex(8).brighten(0)
},
{
category: 'Second',
fromDate: '01.02.2022',
toDate: '01.05.2022',
fromDateReal: '01.01.2022',
toDateReal: '01.06.2022',
colorPlan: colorSet.getIndex(0).brighten(0),
colorReal: colorSet.getIndex(8).brighten(0)
}
]
var categoryAxisGantt = gantt.yAxes.push(new am4charts.CategoryAxis());
categoryAxisGantt.dataFields.category = "category";
categoryAxisGantt.renderer.grid.template.location = 0;
categoryAxisGantt.renderer.inversed = true;
var yLabelGantt = categoryAxisGantt.renderer.labels.template;
yLabelGantt.wrap = true;
yLabelGantt.maxWidth = 200;
var dateAxisGantt = gantt.xAxes.push(new am4charts.DateAxis());
dateAxisGantt.dateFormatter.dateFormat = "dd.MM.yyyy";
dateAxisGantt.renderer.minGridDistance = 70;
dateAxisGantt.min = (new Date(2022, 01, 01)).getTime();
dateAxisGantt.strictMinMax = true;
dateAxisGantt.renderer.tooltipLocation = 0;
var rangeGantt = dateAxisGantt.axisRanges.create();
rangeGantt.date = new Date();
rangeGantt.grid.stroke = am4core.color("red");
rangeGantt.grid.strokeWidth = 2;
rangeGantt.grid.strokeOpacity = 1;
function createGanttSeries(start, end, category, name, color){
var series1Gantt = gantt.series.push(new am4charts.ColumnSeries());
series1Gantt.columns.template.width = am4core.percent(80);
series1Gantt.columns.template.tooltipText = "{name}\n{category}: {openDateX} - {dateX}";
series1Gantt.dataFields.openDateX = start;
series1Gantt.dataFields.dateX = end;
series1Gantt.dataFields.categoryY = category;
series1Gantt.name = name
series1Gantt.columns.template.propertyFields.fill = color; // get color from data
series1Gantt.columns.template.propertyFields.stroke = color;
series1Gantt.columns.template.strokeOpacity = 1;
}
//план
createGanttSeries("fromDate", "toDate", "category", "План", "colorPlan");
//факт
createGanttSeries("fromDateReal", "toDateReal", "category", "Факт", "colorReal");
gantt.scrollbarX = new am4core.Scrollbar();
// Create chart instance
var chart = am4core.create("dds", am4charts.XYChart);
let title = chart.titles.create();
title.text = "ДДС";
title.fontSize = 25;
title.marginBottom = 30;
// Export
chart.exporting.menu = new am4core.ExportMenu();
var dateAxis = chart.xAxes.push(new am4charts.DateAxis());
dateAxis.dateFormatter = new am4core.DateFormatter();
chart.dateFormatter.dateFormat = "MMMM yy";
var valueAxis = chart.yAxes.push(new am4charts.ValueAxis());
var columnSeries = chart.series.push(new am4charts.ColumnSeries());
columnSeries.name = "ДДС (план)";
columnSeries.dataFields.valueY = "val";
columnSeries.dataFields.dateX = "date";
columnSeries.columns.template.tooltipText = "[#fff font-size: 15px]{name} {dateX}:\n[/][#fff font-size: 20px]{valueY}[/] [#fff]{additional}[/]"
columnSeries.columns.template.propertyFields.fillOpacity = "fillOpacity";
columnSeries.columns.template.propertyFields.stroke = "stroke";
columnSeries.columns.template.propertyFields.strokeWidth = "strokeWidth";
columnSeries.columns.template.propertyFields.strokeDasharray = "columnDash";
columnSeries.tooltip.label.textAlign = "middle";
chart.data = [
{
"date": "06.10.2022",
"val": 143566570.46,
"total": 143566570.46
},
{
"date": "09.25.2022",
"val": 35891642.62,
"total": 179458213.08
}
];
body {
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol";
}
#gantt {
width: 100%;
height: 500px;
}
#dds {
width: 100%;
height: 500px;
}
<script src="https://www.amcharts.com/lib/4/core.js"></script>
<script src="https://www.amcharts.com/lib/4/charts.js"></script>
<script src="https://www.amcharts.com/lib/4/themes/animated.js"></script>
<div id="gantt"></div>
<div id="dds"></div>

You're missing an inputDateFormat for your second chart.
chart.dateFormatter.inputDateFormat = "MM.dd.yyyy";
// Themes begin
am4core.useTheme(am4themes_animated);
// Themes end
var gantt = am4core.create("gantt", am4charts.XYChart);
gantt.hiddenState.properties.opacity = 0; // this creates initial fade-in
gantt.paddingRight = 30;
gantt.dateFormatter.inputDateFormat = "dd.MM.yyyy";
var colorSet = new am4core.ColorSet();
colorSet.saturation = 0.4;
gantt.data = [
{
category: 'First',
fromDate: '01.01.2022',
toDate: '01.03.2022',
fromDateReal: '01.02.2022',
toDateReal: '01.04.2022',
colorPlan: colorSet.getIndex(0).brighten(0),
colorReal: colorSet.getIndex(8).brighten(0)
},
{
category: 'Second',
fromDate: '01.02.2022',
toDate: '01.05.2022',
fromDateReal: '01.01.2022',
toDateReal: '01.06.2022',
colorPlan: colorSet.getIndex(0).brighten(0),
colorReal: colorSet.getIndex(8).brighten(0)
}
]
var categoryAxisGantt = gantt.yAxes.push(new am4charts.CategoryAxis());
categoryAxisGantt.dataFields.category = "category";
categoryAxisGantt.renderer.grid.template.location = 0;
categoryAxisGantt.renderer.inversed = true;
var yLabelGantt = categoryAxisGantt.renderer.labels.template;
yLabelGantt.wrap = true;
yLabelGantt.maxWidth = 200;
var dateAxisGantt = gantt.xAxes.push(new am4charts.DateAxis());
dateAxisGantt.dateFormatter.dateFormat = "dd.MM.yyyy";
dateAxisGantt.renderer.minGridDistance = 70;
dateAxisGantt.min = (new Date(2022, 01, 01)).getTime();
dateAxisGantt.strictMinMax = true;
dateAxisGantt.renderer.tooltipLocation = 0;
var rangeGantt = dateAxisGantt.axisRanges.create();
rangeGantt.date = new Date();
rangeGantt.grid.stroke = am4core.color("red");
rangeGantt.grid.strokeWidth = 2;
rangeGantt.grid.strokeOpacity = 1;
function createGanttSeries(start, end, category, name, color){
var series1Gantt = gantt.series.push(new am4charts.ColumnSeries());
series1Gantt.columns.template.width = am4core.percent(80);
series1Gantt.columns.template.tooltipText = "{name}\n{category}: {openDateX} - {dateX}";
series1Gantt.dataFields.openDateX = start;
series1Gantt.dataFields.dateX = end;
series1Gantt.dataFields.categoryY = category;
series1Gantt.name = name
series1Gantt.columns.template.propertyFields.fill = color; // get color from data
series1Gantt.columns.template.propertyFields.stroke = color;
series1Gantt.columns.template.strokeOpacity = 1;
}
//план
createGanttSeries("fromDate", "toDate", "category", "План", "colorPlan");
//факт
createGanttSeries("fromDateReal", "toDateReal", "category", "Факт", "colorReal");
gantt.scrollbarX = new am4core.Scrollbar();
// Create chart instance
var chart = am4core.create("dds", am4charts.XYChart);
let title = chart.titles.create();
title.text = "ДДС";
title.fontSize = 25;
title.marginBottom = 30;
// Export
chart.exporting.menu = new am4core.ExportMenu();
chart.dateFormatter.inputDateFormat = "MM.dd.yyyy";
var dateAxis = chart.xAxes.push(new am4charts.DateAxis());
dateAxis.dateFormatter = new am4core.DateFormatter();
chart.dateFormatter.dateFormat = "MMMM yy";
var valueAxis = chart.yAxes.push(new am4charts.ValueAxis());
var columnSeries = chart.series.push(new am4charts.ColumnSeries());
columnSeries.name = "ДДС (план)";
columnSeries.dataFields.valueY = "val";
columnSeries.dataFields.dateX = "date";
columnSeries.columns.template.tooltipText = "[#fff font-size: 15px]{name} {dateX}:\n[/][#fff font-size: 20px]{valueY}[/] [#fff]{additional}[/]"
columnSeries.columns.template.propertyFields.fillOpacity = "fillOpacity";
columnSeries.columns.template.propertyFields.stroke = "stroke";
columnSeries.columns.template.propertyFields.strokeWidth = "strokeWidth";
columnSeries.columns.template.propertyFields.strokeDasharray = "columnDash";
columnSeries.tooltip.label.textAlign = "middle";
chart.data = [
{
"date": "06.10.2022",
"val": 143566570.46,
"total": 143566570.46
},
{
"date": "09.25.2022",
"val": 35891642.62,
"total": 179458213.08
}
];
body {
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol";
}
#gantt {
width: 100%;
height: 500px;
}
#dds {
width: 100%;
height: 500px;
}
<script src="https://www.amcharts.com/lib/4/core.js"></script>
<script src="https://www.amcharts.com/lib/4/charts.js"></script>
<script src="https://www.amcharts.com/lib/4/themes/animated.js"></script>
<div id="gantt"></div>
<div id="dds"></div>

Related

Amcharts V4 plating a chart in percent rather than absolute using valueYShow

I'm trying to create a chart using AMCharts V4. The values are given in absolute, and I want to have the chart in percentages: values, y-axis, etc.
The docs are here: https://www.amcharts.com/docs/v4/tutorials/plotting-series-from-calculated-values/
However, I'm having some trouble. Pls consider the following codepen forked from the official AMCharts documentation:
https://codepen.io/otmezger/pen/RwVzmjv
As the docs suggest, I have valueAxis.calculateTotals = true; enabled.
The line series.dataFields.valueYShow = "totalPercent"; or series.dataFields.valueYShow = "percent"; renders the graph useless.
only if I disable them, the graph will show in absolute numbers.
what am I missing? how can I make series.dataFields.valueYShow = "percent"; work?
If you want to use percent values in the series, you need to set calculatePercent to true on the series as explained here
series.calculatePercent = true;
series.dataFields.valueYShow = "percent";
series.tooltipText = "{dateX}: [b]{valueY.percent}[/]%";
Demo:
// Create chart instance
var chart = am4core.create("chartdiv", am4charts.XYChart);
// Add data
chart.data = [{
"date": new Date(2018, 0, 1),
"value": 10
}, {
"date": new Date(2018, 0, 2),
"value": 15
}, {
"date": new Date(2018, 0, 3),
"value": 2
}];
// Create axes
var dateAxis = chart.xAxes.push(new am4charts.DateAxis());
dateAxis.renderer.grid.template.location = 0;
dateAxis.renderer.minGridDistance = 30;
var valueAxis = chart.yAxes.push(new am4charts.ValueAxis());
valueAxis.calculateTotals = true;
valueAxis.renderer.labels.template.adapter.add("text", function(text) {
return text + "%";
});
// Create series
var series = chart.series.push(new am4charts.LineSeries());
series.dataFields.valueY = "value";
series.dataFields.dateX = "date";
series.calculatePercent = true;
series.dataFields.valueYShow = "percent";
series.tooltipText = "{dateX}: [b]{valueY.percent}[/]%";
series.strokeWidth = 2;
var bullet = series.bullets.push(new am4charts.CircleBullet());
bullet.circle.stroke = am4core.color("#fff");
bullet.circle.strokeWidth = 2;
// Finish up setting chart up
chart.cursor = new am4charts.XYCursor();
body {
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol";
}
#chartdiv {
width: 100%;
height: 250px;
}
<script src="//www.amcharts.com/lib/4/core.js"></script>
<script src="//www.amcharts.com/lib/4/charts.js"></script>
<div id="chartdiv"></div>

amcharts - XY chart's legend - possibility to distinguish onclick by checkbox and label

Is there any way to distinguish those two behaviors from each other? I would like to obtain different functionality clicking on the legend's checkbox and the other one on the label.
Pic for clarity:
Thanks for any advice
Take a look at these two issues on GitHub:
Legent marker event · Issue #3924
Question: Prevent default hit handler for legend labels · Issue #782
You can do something like that:
chart.legend.markers.template.interactionsEnabled = true;
chart.legend.labels.template.interactionsEnabled = true;
chart.legend.markers.template.events.on("hit", e => {
console.log("Marker!");
});
chart.legend.labels.template.events.on("hit", e => {
console.log("Label!");
});
If we add the code above to this demo, we get the following result:
/**
* ---------------------------------------
* This demo was created using amCharts 4.
*
* For more information visit:
* https://www.amcharts.com/
*
* Documentation is available at:
* https://www.amcharts.com/docs/v4/
* ---------------------------------------
*/
// Themes begin
am4core.useTheme(am4themes_animated);
// Themes end
// Create chart instance
var chart = am4core.create("chartdiv", am4charts.XYChart);
// Add data
chart.data = [ {
"year": "2003",
"europe": 2.5,
"namerica": 2.5,
"asia": 2.1,
"lamerica": 1.2,
"meast": 0.2,
"africa": 0.1
}, {
"year": "2004",
"europe": 2.6,
"namerica": 2.7,
"asia": 2.2,
"lamerica": 1.3,
"meast": 0.3,
"africa": 0.1
}, {
"year": "2005",
"europe": 2.8,
"namerica": 2.9,
"asia": 2.4,
"lamerica": 1.4,
"meast": 0.3,
"africa": 0.1
} ];
// Create axes
var categoryAxis = chart.xAxes.push(new am4charts.CategoryAxis());
categoryAxis.dataFields.category = "year";
categoryAxis.title.text = "Local country offices";
categoryAxis.renderer.grid.template.location = 0;
categoryAxis.renderer.minGridDistance = 20;
categoryAxis.renderer.cellStartLocation = 0.1;
categoryAxis.renderer.cellEndLocation = 0.9;
var valueAxis = chart.yAxes.push(new am4charts.ValueAxis());
valueAxis.min = 0;
valueAxis.title.text = "Expenditure (M)";
// Create series
function createSeries(field, name, stacked) {
var series = chart.series.push(new am4charts.ColumnSeries());
series.dataFields.valueY = field;
series.dataFields.categoryX = "year";
series.name = name;
series.columns.template.tooltipText = "{name}: [bold]{valueY}[/]";
series.stacked = stacked;
series.columns.template.width = am4core.percent(95);
}
createSeries("europe", "Europe", false);
createSeries("namerica", "North America", true);
createSeries("asia", "Asia", false);
createSeries("lamerica", "Latin America", true);
createSeries("meast", "Middle East", true);
createSeries("africa", "Africa", true);
// Add legend
chart.legend = new am4charts.Legend();
chart.legend.useDefaultMarker = true;
const marker = chart.legend.markers.template;
const markerColumn = marker.children.getIndex(0);
const markerColumnActiveState = markerColumn.states.getKey("active");
// When a legend item is disabled it triggers its active state,
// test it out:
// markerColumnActiveState.properties.strokeWidth = 2;
// markerColumnActiveState.properties.stroke = "#000";
// markerColumnActiveState.properties.strokeOpacity = 1;
// markerColumnActiveState.adapter.add("fill", function() {
// return am4core.color("red");
// });
markerColumn.cornerRadius(0, 0, 0, 0);
markerColumn.defaultState.properties.fillOpacity = 0;
markerColumn.defaultState.properties.strokeWidth = 1;
markerColumn.defaultState.properties.stroke = am4core.color("#000");
markerColumn.defaultState.properties.strokeOpacity = 1;
// markerColumnActiveState.properties.fillOpacity = 0;
// Add custom image instead
const checkbox = marker.createChild(am4core.Image);
checkbox.width = 20;
checkbox.height = 20;
checkbox.verticalCenter = "top";
checkbox.horizontalCenter = "left";
checkbox.href = "https://cdn.onlinewebfonts.com/svg/img_207414.png";
checkbox.dx = 1;
checkbox.dy = 1;
const checkboxActiveState = checkbox.states.create("active");
checkboxActiveState.properties.opacity = 0;
// ========================= HERE =========================
chart.legend.markers.template.interactionsEnabled = true;
chart.legend.labels.template.interactionsEnabled = true;
chart.legend.markers.template.events.on("hit", e => {
console.log("Marker!");
});
chart.legend.labels.template.events.on("hit", e => {
console.log("Label!");
});
body {
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol";
}
#chartdiv {
width: 100%;
height: 500px;
}
<!--
fork of:
https://www.amcharts.com/demos/stacked-clustered-column-chart/
answering:
https://stackoverflow.com/questions/56652882/how-to-make-amcharts-4-legend-having-a-toggleable-checkbox
-->
<script src="https://www.amcharts.com/lib/4/core.js"></script>
<script src="https://www.amcharts.com/lib/4/charts.js"></script>
<script src="https://www.amcharts.com/lib/4/themes/animated.js"></script>
<div id="chartdiv"></div>

amCharts 4: External Data Source - JSON Output from Database

Still trying to get to grips with amCharts 4 using external data sources, and this is a follow on from my previous question... amCharts 4: External Data Source
I've created a horizontal stacked chart using static data, which works fine (code snippet below).
However, when using the external data source on the same chart, I don't get same result, as the JSON output is different.
This is because the external database table columns and rows are the opposite way around (from the static data) and unfortunately can't be changed at source.
I've gone through the amCharts documentation and demo charts, and tried all sorts to achieve the same chart as the static data version with no success.
e.g. Tried the inversed opposite rotate properties, as well as reconfiguring the chart series and axes.
Maybe, the parseended event can be used to re-map the external data, but to be honest I don't know how.
I've come to a dead-end, and not sure how, or what is the correct (or best practice) method of doing this?
The code (and data structure) for each chart is shown below.
Any help would be very much appreciated. Thanks in advance.
STATIC DATA CHART (Correct)
// Create chart instance
var chart = am4core.create("chartdiv", am4charts.XYChart);
// Add data
var chartdata = [{
"Name": "Question 1 Shows Here",
"Yes Please": 75,
"Maybe": 45,
"No Thanks": 19
}, {
"Name": "Question 2 Shows Here",
"Yes Please": 35,
"Maybe": 43,
"No Thanks": 26
}, {
"Name": "Question 3 Shows Here",
"Yes Please": 57,
"Maybe": 24,
"No Thanks": 8
}];
chart.data = chartdata;
// Create axes
var categoryAxis = chart.yAxes.push(new am4charts.CategoryAxis());
categoryAxis.dataFields.category = "Name";
categoryAxis.renderer.grid.template.location = 0;
categoryAxis.renderer.minGridDistance = 20;
var valueAxis = chart.xAxes.push(new am4charts.ValueAxis());
valueAxis.numberFormatter.numberFormat = "#";
valueAxis.min = 0;
valueAxis.transitionDuration = 200;
valueAxis.interpolationDuration = 200;
valueAxis.rangeChangeDuration = 200;
// Create series
var series = chart.series.push(new am4charts.ColumnSeries());
series.dataFields.valueX = "Yes Please";
series.dataFields.categoryY = "Name";
series.name = "Yes Please";
series.tooltipText = "{name}: [bold]{valueX}[/]";
series.stacked = true;
var series2 = chart.series.push(new am4charts.ColumnSeries());
series2.dataFields.valueX = "Maybe";
series2.dataFields.categoryY = "Name";
series2.name = "Maybe";
series2.tooltipText = "{name}: [bold]{valueX}[/]";
series2.stacked = true;
var series3 = chart.series.push(new am4charts.ColumnSeries());
series3.dataFields.valueX = "Don’t Know";
series3.dataFields.categoryY = "Name";
series3.name = "Don’t Know";
series3.tooltipText = "{name}: [bold]{valueX}[/]";
series3.stacked = true;
var series4 = chart.series.push(new am4charts.ColumnSeries());
series4.dataFields.valueX = "No Thanks";
series4.dataFields.categoryY = "Name";
series4.name = "No Thanks";
series4.tooltipText = "{name}: [bold]{valueX}[/]";
series4.stacked = true;
// Add cursor
chart.cursor = new am4charts.XYCursor();
chart.cursor.behavior = "zoomXY";
// Add legend
chart.legend = new am4charts.Legend();
chart.legend.position = "bottom";
<script src="//www.amcharts.com/lib/4/core.js"></script>
<script src="//www.amcharts.com/lib/4/charts.js"></script>
<div id="chartdiv" style="width: 90%; height: 300px";></div>
EXTERNAL DATA CHART (Incorrect)
// Create chart instance
var chart = am4core.create("chartdiv", am4charts.XYChart);
// Add data
var chartdata = [{
"Name": "Yes Please",
"Question 1 Shows Here": 75,
"Question 2 Shows Here": 45,
"Question 3 Shows Here": 19
}, {
"Name": "Maybe",
"Branding and Logos Score": 367,
"Question 1 Shows Here": 35,
"Question 2 Shows Here": 43,
"Question 3 Shows Here": 26
}, {
"Name": "Don’t Know",
"Question 1 Shows Here": 42,
"Question 2 Shows Here": 31,
"Question 3 Shows Here": 12
}, {
"Name": "No Thanks",
"Question 1 Shows Here": 17,
"Question 2 Shows Here": 27,
"Question 3 Shows Here": 15
}];
chart.data = chartdata;
// Create category axes
var categoryAxis = chart.yAxes.push(new am4charts.CategoryAxis());
categoryAxis.dataFields.category = "Name";
categoryAxis.renderer.grid.template.location = 0;
categoryAxis.renderer.minGridDistance = 20;
// Create value axes
var valueAxis = chart.xAxes.push(new am4charts.ValueAxis());
valueAxis.numberFormatter.numberFormat = "#";
valueAxis.min = 0;
valueAxis.transitionDuration = 200;
valueAxis.interpolationDuration = 200;
valueAxis.rangeChangeDuration = 200;
// Create series
var series = chart.series.push(new am4charts.ColumnSeries());
series.dataFields.valueX = "Question 1 Shows Here";
series.dataFields.categoryY = "Name";
series.name = "Question 1 Shows Here";
series.tooltipText = "{name}: [bold]{valueX}[/]";
series.stacked = true;
var series2 = chart.series.push(new am4charts.ColumnSeries());
series2.dataFields.valueX = "Question 2 Shows Here";
series2.dataFields.categoryY = "Name";
series2.name = "Question 2 Shows Here";
series2.tooltipText = "{name}: [bold]{valueX}[/]";
series2.stacked = true;
var series3 = chart.series.push(new am4charts.ColumnSeries());
series3.dataFields.valueX = "Question 3 Shows Here";
series3.dataFields.categoryY = "Name";
series3.name = "Question 3 Shows Here";
series3.tooltipText = "{name}: [bold]{valueX}[/]";
series3.stacked = true;
// Add cursor
chart.cursor = new am4charts.XYCursor();
chart.cursor.behavior = "zoomXY";
chart.legend = new am4charts.Legend();
chart.legend.position = "bottom";
<script src="//www.amcharts.com/lib/4/core.js"></script>
<script src="//www.amcharts.com/lib/4/charts.js"></script>
<div id="chartdiv" style="width: 90%; height: 300px";></div>
parseended is the way to go in this situation. You'll need to set up a lookup table for your questions and map them to objects containing the values mapped to each response, for example:
chart.dataSource.events.on("parseended", function(ev) {
var questionMap = {}; //lookup table to map questions to data elements
ev.target.data.forEach(function(item) {
Object.keys(item).forEach(function(key) {
if (key.indexOf('Name') == -1) { //act on non-response keys
if (!questionMap[key]) {
questionMap[key] = {"Name": key}; //create an object containing the name/question pair if it doesn't exist
}
questionMap[key][item.Name] = item[key]; // assign response+value to the object (e.g. "Yes, Please": 75)
}
});
});
//remap lookup table as array
ev.target.data = Object.keys(questionMap).map(function(question) {
return questionMap[question];
});
});
Demo below:
// Create chart instance
var chart = am4core.create("chartdiv", am4charts.XYChart);
chart.dataSource.url = dataURI(); //fake URL for demonstration purposes
chart.dataSource.events.on("parseended", function(ev) {
var questionMap = {}; //lookup table to map questions to data elements
ev.target.data.forEach(function(item) {
Object.keys(item).forEach(function(key) {
if (key.indexOf('Name') == -1) { //act on non-response keys
if (!questionMap[key]) {
questionMap[key] = {
"Name": key
}; //create an object containing the name/question pair if it doesn't exist
}
questionMap[key][item.Name] = item[key]; // assign response+value to the object (e.g. "Yes, Please": 75)
}
});
});
//remap lookup table as array
ev.target.data = Object.keys(questionMap).map(function(question) {
return questionMap[question];
});
});
// Create axes
var categoryAxis = chart.yAxes.push(new am4charts.CategoryAxis());
categoryAxis.dataFields.category = "Name";
categoryAxis.renderer.grid.template.location = 0;
categoryAxis.renderer.minGridDistance = 20;
var valueAxis = chart.xAxes.push(new am4charts.ValueAxis());
valueAxis.numberFormatter.numberFormat = "#";
valueAxis.min = 0;
valueAxis.transitionDuration = 200;
valueAxis.interpolationDuration = 200;
valueAxis.rangeChangeDuration = 200;
// Create series
var series = chart.series.push(new am4charts.ColumnSeries());
series.dataFields.valueX = "Yes Please";
series.dataFields.categoryY = "Name";
series.name = "Yes Please";
series.tooltipText = "{name}: [bold]{valueX}[/]";
series.stacked = true;
var series2 = chart.series.push(new am4charts.ColumnSeries());
series2.dataFields.valueX = "Maybe";
series2.dataFields.categoryY = "Name";
series2.name = "Maybe";
series2.tooltipText = "{name}: [bold]{valueX}[/]";
series2.stacked = true;
var series3 = chart.series.push(new am4charts.ColumnSeries());
series3.dataFields.valueX = "Don’t Know";
series3.dataFields.categoryY = "Name";
series3.name = "Don’t Know";
series3.tooltipText = "{name}: [bold]{valueX}[/]";
series3.stacked = true;
var series4 = chart.series.push(new am4charts.ColumnSeries());
series4.dataFields.valueX = "No Thanks";
series4.dataFields.categoryY = "Name";
series4.name = "No Thanks";
series4.tooltipText = "{name}: [bold]{valueX}[/]";
series4.stacked = true;
// Add cursor
chart.cursor = new am4charts.XYCursor();
chart.cursor.behavior = "zoomXY";
// Add legend
chart.legend = new am4charts.Legend();
chart.legend.position = "bottom";
function dataURI() {
return "data:application/json;base64,W3sKICAiTmFtZSI6ICJZZXMgUGxlYXNlIiwKICAiUXVlc3Rpb24gMSBTaG93cyBIZXJlIjogNzUsCiAgIlF1ZXN0aW9uIDIgU2hvd3MgSGVyZSI6IDQ1LAogICJRdWVzdGlvbiAzIFNob3dzIEhlcmUiOiAxOQp9LCB7CiAgIk5hbWUiOiAiTWF5YmUiLAogICJCcmFuZGluZyBhbmQgTG9nb3MgU2NvcmUiOiAzNjcsCiAgIlF1ZXN0aW9uIDEgU2hvd3MgSGVyZSI6IDM1LAogICJRdWVzdGlvbiAyIFNob3dzIEhlcmUiOiA0MywKICAiUXVlc3Rpb24gMyBTaG93cyBIZXJlIjogMjYKfSwgewogICJOYW1lIjogIkRvbuKAmXQgS25vdyIsCiAgIlF1ZXN0aW9uIDEgU2hvd3MgSGVyZSI6IDQyLAogICJRdWVzdGlvbiAyIFNob3dzIEhlcmUiOiAzMSwKICAiUXVlc3Rpb24gMyBTaG93cyBIZXJlIjogMTIKfSwgewogICJOYW1lIjogIk5vIFRoYW5rcyIsCiAgIlF1ZXN0aW9uIDEgU2hvd3MgSGVyZSI6IDE3LAogICJRdWVzdGlvbiAyIFNob3dzIEhlcmUiOiAyNywKICAiUXVlc3Rpb24gMyBTaG93cyBIZXJlIjogMTUKfV0=";
}
<script src="//www.amcharts.com/lib/4/core.js"></script>
<script src="//www.amcharts.com/lib/4/charts.js"></script>
<div id="chartdiv" style="width: 90%; height: 300px" ;></div>

Remove vertical label axis of Multiple-Value-Axes amchart

I am okay all the line types under graph but I don't need their vertical represents
morever I have only one axis and yaxis values in my chart but multiple lines.
How can I customize this amchart sample or find any suitable chart for my need?
Practically every component in the chart has a disabled property that you can use to hide or reveal. To get rid of the line and labels, simply set the disabled property to true to remove them, similar to how the grid was disabled:
valueAxis.renderer.line.disabled = true; //disables axis line
valueAxis.renderer.labels.template.disabled = true; //disables labels
valueAxis.renderer.grid.template.disabled = true; //disables grid
Demo:
// Create chart instance
var chart = am4core.create("chartdiv", am4charts.XYChart);
// Increase contrast by taking evey second color
chart.colors.step = 2;
// Add data
chart.data = generateChartData();
// Create axes
var dateAxis = chart.xAxes.push(new am4charts.DateAxis());
// Create series
function createAxisAndSeries(field, name, bullet) {
var valueAxis = chart.yAxes.push(new am4charts.ValueAxis());
var series = chart.series.push(new am4charts.LineSeries());
series.dataFields.valueY = field;
series.dataFields.dateX = "date";
series.strokeWidth = 2;
series.yAxis = valueAxis;
series.name = name;
series.tooltipText = "{name}: [bold]{valueY}[/]";
series.tensionX = 0.8;
var interfaceColors = new am4core.InterfaceColorSet();
switch(bullet) {
case "triangle":
var bullet = series.bullets.push(new am4charts.Bullet());
bullet.width = 12;
bullet.height = 12;
bullet.horizontalCenter = "middle";
bullet.verticalCenter = "middle";
var triangle = bullet.createChild(am4core.Triangle);
triangle.stroke = interfaceColors.getFor("background");
triangle.strokeWidth = 2;
triangle.direction = "top";
triangle.width = 12;
triangle.height = 12;
break;
case "rectangle":
var bullet = series.bullets.push(new am4charts.Bullet());
bullet.width = 10;
bullet.height = 10;
bullet.horizontalCenter = "middle";
bullet.verticalCenter = "middle";
var rectangle = bullet.createChild(am4core.Rectangle);
rectangle.stroke = interfaceColors.getFor("background");
rectangle.strokeWidth = 2;
rectangle.width = 10;
rectangle.height = 10;
break;
default:
var bullet = series.bullets.push(new am4charts.CircleBullet());
bullet.circle.stroke = interfaceColors.getFor("background");
bullet.circle.strokeWidth = 2;
break;
}
valueAxis.renderer.line.disabled = true;
valueAxis.renderer.labels.template.disabled = true;
valueAxis.renderer.grid.template.disabled = true;
}
createAxisAndSeries("visits", "Visits", "circle");
createAxisAndSeries("views", "Views", "triangle");
createAxisAndSeries("hits", "Hits", "rectangle");
// Add legend
chart.legend = new am4charts.Legend();
// Add cursor
chart.cursor = new am4charts.XYCursor();
// generate some random data, quite different range
function generateChartData() {
var chartData = [];
var firstDate = new Date();
firstDate.setDate(firstDate.getDate() - 100);
firstDate.setHours(0, 0, 0, 0);
var visits = 1600;
var hits = 2900;
var views = 8700;
for (var i = 0; i < 15; i++) {
// we create date objects here. In your data, you can have date strings
// and then set format of your dates using chart.dataDateFormat property,
// however when possible, use date objects, as this will speed up chart rendering.
var newDate = new Date(firstDate);
newDate.setDate(newDate.getDate() + i);
visits += Math.round((Math.random()<0.5?1:-1)*Math.random()*10);
hits += Math.round((Math.random()<0.5?1:-1)*Math.random()*10);
views += Math.round((Math.random()<0.5?1:-1)*Math.random()*10);
chartData.push({
date: newDate,
visits: visits,
hits: hits,
views: views
});
}
return chartData;
}
#chartdiv {
width: 100%;
height: 500px;
}
<script src="https://www.amcharts.com/lib/4/core.js"></script>
<script src="https://www.amcharts.com/lib/4/charts.js"></script>
<div id="chartdiv"></div>
multiple-value-axes remove 3rd axis from chart.
Only two axis will show.

amchart indicator loading not stopping even chart is appear

im try using Amchart plugin into my system. Chart successful appear when i run basic code for chart. Then im try to use a custom for loading indicator. But the problem is indicator for loading not stopping even chart is appear.
Below is the JS script
am4core.useTheme(am4themes_animated);
// Create chart instance
var chart = am4core.create("chartdiv", am4charts.XYChart);
// Add data
chart.data = [{
"category": "Research",
"value": 450
}, {
"category": "Marketing",
"value": 1200
}, {
"category": "Distribution",
"value": 1850
}];
// Create axes
var categoryAxis = chart.xAxes.push(new am4charts.CategoryAxis());
categoryAxis.dataFields.category = "category";
categoryAxis.renderer.grid.template.location = 0;
//categoryAxis.renderer.minGridDistance = 30;
var valueAxis = chart.yAxes.push(new am4charts.ValueAxis());
// Create series
var series = chart.series.push(new am4charts.ColumnSeries());
series.dataFields.valueY = "value";
series.dataFields.categoryX = "category";
var indicator;
var indicatorInterval;
function showIndicator() {
if (!indicator) {
indicator = chart.tooltipContainer.createChild(am4core.Container);
indicator.background.fill = am4core.color("#fff");
indicator.width = am4core.percent(100);
indicator.height = am4core.percent(100);
var indicatorLabel = indicator.createChild(am4core.Label);
indicatorLabel.text = "Loading stuff...";
indicatorLabel.align = "center";
indicatorLabel.valign = "middle";
indicatorLabel.dy = 50;
var hourglass = indicator.createChild(am4core.Image);
hourglass.href = "https://s3-us-west-2.amazonaws.com/s.cdpn.io/t-160/hourglass.svg";
hourglass.align = "center";
hourglass.valign = "middle";
hourglass.horizontalCenter = "middle";
hourglass.verticalCenter = "middle";
hourglass.scale = 0.7;
}
indicator.hide(0);
indicator.show();
clearInterval(indicatorInterval);
indicatorInterval = setInterval(function() {
hourglass.animate([{
from: 0,
to: 360,
property: "rotation"
}], 2000);
}, 3000);
}
function hideIndicator() {
indicator.hide();
clearInterval(indicatorInterval);
}
showIndicator();
Below is the html code
<script src="//www.amcharts.com/lib/4/core.js"></script>
<script src="//www.amcharts.com/lib/4/charts.js"></script>
<script src="//www.amcharts.com/lib/4/themes/animated.js"></script>
<div id="chartdiv"></div>
im already try a few method, but no lucky for me. Any suggestion
On amchart4 documentation (https://www.amcharts.com/docs/v4/tutorials/custom-loading-indicator/), it only shows how you can create a custom loading indicator. It doesn't hook up hideIndicator() event when the chart is finished rendering.
There is a Ready event you can use now (https://github.com/amcharts/amcharts4/issues/436#issuecomment-441370242). You can just simply hook up hideIndicator() function to the Ready event:
...
chart.events.on("ready", function(ev){
hideIndicator();
});
...
fiddle: https://jsfiddle.net/davidliang2008/akpe5f4b/1/

Resources