Rounded Edge Stacked Grouped Chart in amchart4 - javascript

I've been looking everywhere but still can't find the answer, i have stacked and clustered chart on my website (Using amchart4), like this :
Screenshot for chart in my website
, what i want to is, round the edge of the top the chart, so chart can look like this :
Chart that have rounded corner
, I've been trying to add an adapter, base on documentation in amchart4 : https://www.amcharts.com/docs/v4/tutorials/stacked-column-series-with-rounded-corners/
but instead working like a charm, my chart look like this :
My chart Now!
where's my mistake here?
My Js Code for amchart :
am4core.ready(function() {
// Create chart instance
var chart = am4core.create('regional', am4charts.XYChart);
// Add data
chart.data = response.data;
// Create axes
var categoryAxis = chart.xAxes.push(new am4charts.CategoryAxis());
categoryAxis.renderer.grid.template.disabled = true;
categoryAxis.dataFields.category = "regional";
// Room for sub-category labels
categoryAxis.renderer.labels.template.marginTop = 20;
var valueAxis = chart.yAxes.push(new am4charts.ValueAxis());
valueAxis.min = 0;
// Create series
function createSeries(field, name, stacked, color) {
var series = chart.series.push(new am4charts.ColumnSeries());
series.dataFields.valueY = field;
series.dataFields.categoryX = "regional";
series.name = name;
series.stroke = am4core.color(color);
series.fill = am4core.color(color);
// series.columns.template.tooltipText = "{name}: [bold]{valueY}[/]";
series.stacked = stacked;
series.columns.template.column.adapter.add("cornerRadiusTopLeft", cornerRadius);
series.columns.template.column.adapter.add("cornerRadiusTopRight", cornerRadius);
var bullet1 = series.bullets.push(new am4charts.LabelBullet());
bullet1.interactionsEnabled = false;
bullet1.label.text = "{valueY}";
bullet1.label.fontSize = 19;
bullet1.label.fill = am4core.color("#0d0b0b");
bullet1.locationY = 0.5;
}
// Fake series (display cluster label)
function createLabelSeries(name) {
var series = chart.series.push(new am4charts.ColumnSeries());
series.dataFields.valueY = "zero";
series.dataFields.categoryX = "regional";
series.name = name;
series.hiddenInLegend = true;
var bullet = series.bullets.push(new am4charts.LabelBullet());
bullet.label.text = "{name}";
bullet.label.hideOversized = false;
bullet.label.paddingTop = 30;
}
// Corner
function cornerRadius(radius, item) {
var dataItem = item.dataItem;
// Find the last series in this stack
var lastSeries;
chart.series.each(function(series) {
if (dataItem.dataContext[series.dataFields.valueY] && !series.isHidden && !series.isHiding) {
lastSeries = series;
}
});
// console.log(lastSeries.name);
// If current series is the one, use rounded corner
return dataItem.component == lastSeries ? 10 : radius;
}
chart.maskBullets = false;
createLabelSeries("OPEN");
createSeries("NCR_OPEN", "NCR OPEN", true, "#ff955c");
createSeries("HCR_OPEN", "HCR OPEN", true, "#ff7429");
createSeries("PCR_OPEN", "PCR OPEN", true, "#fa4300");
createLabelSeries("CLOSE");
createSeries("NCR_CLOSE", "NCR CLOSE", true, "#8870ff");
createSeries("HCR_CLOSE", "HCR CLOSE", true, "#5d3dff");
createSeries("PCR_CLOSE", "PCR CLOSE", true, "#370fff");
// Legend
chart.legend = new am4charts.Legend();
})

Related

AM charts display fixed range in axes

Really Hope you can help!
I am using AMCharts and Ive added some data through a simple php function (pulled from database)
I've added some upper and lower limits, using the below function
// HORIZONTAL GREEN RANGE
var guide = new AmCharts.Guide();
guide.value =11.00;
guide.toValue = 11.80;
guide.fillColor = "#00CC00";
guide.inside = true;
guide.fillAlpha = 0.2;
guide.lineAlpha = 0;
valueAxis.addGuide(guide);
var guideRED = new AmCharts.Guide();
guideRED.value = 11.80;
guideRED.toValue = 11.90;
guideRED.fillColor = "orange";
guideRED.inside = true;
guideRED.fillAlpha = 0.2;
guideRED.lineAlpha = 0;
valueAxis.addGuide(guideRED);
var guideblue = new AmCharts.Guide();
guideblue.value = 11.00;
guideblue.toValue = 10.90;
guideblue.fillColor = "orange";
guideblue.inside = true;
guideblue.fillAlpha = 0.2;
guideblue.lineAlpha = 0;
valueAxis.addGuide(guideblue);
but I need to set the display range on the Y (top to bottom???) to 11.90 and 10.50 respectivly so the operator can see the plotted line, and its relationship to the red and orange zones. At present the graph simply renders the chart to +1 the max and min it has in value form.
Here is the graph functions
AmCharts.ready(function () {
// SERIAL CHART
chart = new AmCharts.AmSerialChart();
chart.dataProvider = chartData;
chart.dataDateFormat = "DD-MM-YY";
chart.categoryField = "date";
// AXES
// category
var categoryAxis = chart.categoryAxis;
// categoryAxis.parseDates = true; // as our data is date-based, we set parseDates to true
// categoryAxis.minPeriod = "DD"; // our data is daily, so we set minPeriod to DD
categoryAxis.gridAlpha = 0.1;
categoryAxis.minorGridAlpha = 0.1;
categoryAxis.axisAlpha = 0;
categoryAxis.minorGridEnabled = true;
categoryAxis.inside = true;
// value
var valueAxis = new AmCharts.ValueAxis();
valueAxis.tickLength = 4;
valueAxis.axisAlpha = 0;
valueAxis.showFirstLabel = false;
valueAxis.showLastLabel = false;
valueAxis.autoGridCount = true;
valueAxis.gridCount = 500;
chart.addValueAxis(valueAxis);
// GRAPH
var graph = new AmCharts.AmGraph();
graph.dashLength = 2;
graph.lineColor = "#00CC00";
graph.valueField = "value";
graph.dashLength = 3;
graph.bullet = "round";
graph.balloonText = "[[category]]<br><b><span style=\'font-size:14px;\'>value:[[value]]</span></b>";
chart.addGraph(graph);
(this is all within a tag hence the escape char's
Many thanks.
To force the value axis to particular scale, first set it's minimum and maximum properties. The chart will still try to use whole numbers. To disable that set strictMinMax.
I.e.:
var valueAxis = new AmCharts.ValueAxis();
valueAxis.minimum = 10.5;
valueAxis.maximum = 11.90;
valueAxis.strictMinMax = true;

Amchart - Export to PNG file

I created an amchart for plotting time based area. I need to add an export to image option to this graph. Below shows my amchart code. What are the lines needed to add the export to image option to this graph
AmCharts.ready(function () {
// first we generate some random data
generateChartData();
// SERIAL CHART
chart = new AmCharts.AmSerialChart();
chart.pathToImages = "../amcharts/images/";
chart.dataProvider = chartData;
chart.categoryField = "date";
// data updated event will be fired when chart is first displayed,
// also when data will be updated. We'll use it to set some
// initial zoom
chart.addListener("dataUpdated", zoomChart);
// AXES
// Category
var categoryAxis = chart.categoryAxis;
categoryAxis.parseDates = true; // in order char to understand dates, we should set parseDates to true
categoryAxis.minPeriod = "mm"; // as we have data with minute interval, we have to set "mm" here.
categoryAxis.gridAlpha = 0.07;
categoryAxis.axisColor = "#DADADA";
// Value
var valueAxis = new AmCharts.ValueAxis();
valueAxis.gridAlpha = 0.07;
valueAxis.title = "Unique visitors";
chart.addValueAxis(valueAxis);
// GRAPH
var graph = new AmCharts.AmGraph();
graph.type = "line"; // try to change it to "column"
graph.title = "red line";
graph.valueField = "visits";
graph.lineAlpha = 1;
graph.lineColor = "#d1cf2a";
graph.fillAlphas = 0.3; // setting fillAlphas to > 0 value makes it area graph
chart.addGraph(graph);
// CURSOR
var chartCursor = new AmCharts.ChartCursor();
chartCursor.cursorPosition = "mouse";
chartCursor.categoryBalloonDateFormat = "JJ:NN, DD MMMM";
chart.addChartCursor(chartCursor);
// SCROLLBAR
var chartScrollbar = new AmCharts.ChartScrollbar();
chart.addChartScrollbar(chartScrollbar);
// WRITE
chart.write("chartdiv");
});
You should just be able to add the following before you write the chart to the DIV.
"exportConfig":{
"menuTop": 0,
menuItems: [{
textAlign: 'center',
icon: 'images/graph_export.png',
iconTitle: 'Save chart as an image',
onclick:function(){},
items: [
{title:'JPG', format:'jpg'},
{title:'PNG', format:'png'},
{title:'SVG', format:'svg'}
]
}]
}
This will give you a download icon on the graph to download in either JPG, PNG or SVG formats.
Try this code :
chart.export = {
enabled: true,
position: "bottom-right"
}
chart.initHC = false;
chart.validateNow();
And don't forget to include the needed export plugin!

amcharts showing value inside bar

I'm using amCharts, and i want to show values inside bar
This is how it looks at the moment:
and I want it to be like this:
This is my code to display chart:
AmCharts.ready(function() {
generateWidgetData('week');
// SERIAL CHART
chart = new AmCharts.AmSerialChart();
chart.dataProvider = graphData;
chart.categoryField = 'date';
chart.startDuration = 1;
chart.columnWidth = 0.60;
chart.dataDateFormat = 'YYYY-MM-DD';
chart.startEffect = 'easeInSine';
chart.stackType = 'regular';
// AXES
// category
var categoryAxis = chart.categoryAxis;
categoryAxis.parseDates = true;
categoryAxis.minPeriod = 'DD';
categoryAxis.plotAreaBorderAlpha = 0.01;
categoryAxis.labelRotation = 90;
categoryAxis.axisThickness = 0;
categoryAxis.stackType = 'regular';
categoryAxis.gridThickness = 0;
categoryAxis.inside = false;
//categoryAxis.gridPosition = 'start';
//categoryAxis.startDate = '2014-05-08';
// value
// in case you don't want to change default settings of value axis,
// you don't need to create it, as one value axis is created automatically.
// GRAPH
var graph = new AmCharts.AmGraph();
graph.maxColumns = 1;
graph.valueField = 'Self-entered';
graph.balloonText = '[[category]]: <b>[[value]]</b>';
graph.type = 'column';
graph.lineAlpha = 0;
graph.labelText = '[[value]]';
graph.fillAlphas = 0.8;
graph.stackType = 'regular';
chart.addGraph(graph);
graph.cornerRadiusTop = 8;
// CURSOR
var chartCursor = new AmCharts.ChartCursor();
chartCursor.cursorAlpha = 0;
chartCursor.zoomable = false;
chartCursor.categoryBalloonEnabled = false;
chart.addChartCursor(chartCursor);
chart.creditsPosition = 'top-right';
chart.write('stepschart');
});
Thanks in advance.
I fixed this so i will post answer, maybe it will help to someone
its really simple all you need to do is to add 2 lines of code:
graph.labelText = '[[value]]'; // this will insert values in labels
graph.labelPosition = 'inside'; // and with this we put our label inside bar
Hope this will help to someone who also need to do same thing

AmChart not showing second y-axis

I have been assigned a task of displaying 2 charts in a graph, therefore I need two y-axis to represent the charts.
One axis will be on the left, the other on the right.
My problem is that only one y axis is showing.
As I am using amCharts, I have noticed one issue where the visible y Axis has an array called allLabels and it has elements in it, but the second y Axis does not, however I don't know
what fills up that array property.
Code:
function addconsumerUsageGraph(consumerUsageGraph) {
//You can ignore this
if (consumerUsageGraph.graphs.length == 2)
{
var gr1 = consumerUsageGraph.graphs[0];
var gr2 = consumerUsageGraph.graphs[1];
consumerUsageGraph.removeGraph(gr1);
consumerUsageGraph.removeGraph(gr2);
}
if (consumerUsageGraph.graphs.length == 1) {
var gr1 = consumerUsageGraph.graphs[0];
consumerUsageGraph.removeGraph(gr1);
}
//I add the two yAxis here:
var yAxis1 = new AmCharts.ValueAxis();
yAxis1.position = "left";
var yAxis2 = new AmCharts.ValueAxis();
yAxis2.position = "right"; // set this to "right" if you want it on the right
//Adding the value axis to the whole graph
consumerUsageGraph.addValueAxis(yAxis1);
consumerUsageGraph.addValueAxis(yAxis2);
var graph = new AmCharts.AmGraph();
graph.valueField = "value";
graph.type = "column";
graph.title = "Usage";
graph.lineAlpha = 1;
graph.fillAlphas = 0.75;
consumerUsageGraph.addGraph(graph);
var graph2 = new AmCharts.AmGraph();
graph2.valueField = "cost";
graph2.type = "line";
graph2.lineAlpha = 1;
graph2.title = "Cost";
graph2.lineColor = graphColour;
graph2.bullet = "round";
graph2.bulletAlpha = 0.5;
graph2.bulletBorderAlpha = 0.8;
//Assigning the second yAxis to the second graph
graph2.valueAxes = yAxis2;
consumerUsageGraph.addGraph(graph2);
//consumerUsageGraph.write("chartdiv");
var legend = new AmCharts.AmLegend();
legend.marginBottom = -10;
legend.useGraphSettings = true;
consumerUsageGraph.addLegend(legend);
}
How the chart looks now:
graph doesn't have property valueAxes, use valueAxis instead:
graph2.valueAxis = yAxis2;

AmCharts - graphs connected by starting and ending points

I have multiple graphs per chart and for some unknown(to me) reason each graph is connected by the start and the end points of it e.g.
Simple question: How to remove it? I couldn't find anything in the documentation that controls it.
My code as follows:
lineChart = function(id, period) {
var chart, data = [];
var cData = chartData[id];
AmCharts.ready(function() {
for (item in cData) {
var i = cData[item];
data.push({
date: new Date(i.date),
impressions: i.impressions,
clicks: i.clicks,
conversions: i.conversions,
ctr: i.ctr,
profit: i.profit,
cost: i.cost,
revenue: i.revenue
});
}
chart = new AmCharts.AmSerialChart();
chart.dataProvider = data;
chart.categoryField = "date";
chart.balloon.color = "#000000";
// AXES
// category
var categoryAxis = chart.categoryAxis;
categoryAxis.fillAlpha = 1;
categoryAxis.fillColor = "#FAFAFA";
categoryAxis.gridAlpha = 0;
categoryAxis.axisAlpha = 0;
categoryAxis.minPeriod = period;
categoryAxis.parseDates = true;
categoryAxis.gridPosition = "start";
categoryAxis.position = "bottom";
// value
var valueAxis = new AmCharts.ValueAxis();
valueAxis.dashLength = 5;
valueAxis.axisAlpha = 0;
valueAxis.integersOnly = true;
valueAxis.gridCount = 10;
chart.addValueAxis(valueAxis);
// GRAPHS
// Impressions graph
var graph = new AmCharts.AmGraph();
graph.title = "Impressions";
graph.valueField = "impressions";
graph.balloonText = "[[title]]: [[value]]";
//graph.lineAlpha = 1;
graph.bullet = "round";
chart.addGraph(graph);
// Clicks graph
var graph = new AmCharts.AmGraph();
graph.title = "Clicks";
graph.valueField = "clicks";
graph.balloonText = "[[title]]: [[value]]";
graph.bullet = "round";
chart.addGraph(graph);
// Conversions graph
var graph = new AmCharts.AmGraph();
graph.title = "Conversion";
graph.valueField = "conversions";
graph.balloonText = "[[title]]: [[value]]";
graph.bullet = "round";
chart.addGraph(graph);
// LEGEND
var legend = new AmCharts.AmLegend();
legend.markerType = "circle";
chart.addLegend(legend);
var chartCursor = new AmCharts.ChartCursor();
chartCursor.cursorPosition = "mouse";
if(period == 'hh')
chartCursor.categoryBalloonDateFormat = "MMM DD, JJ:00";
chart.addChartCursor(chartCursor);
// WRITE
chart.write(id);
});
};
This looks like the data issue. Make sure your data points are strictly in consecutive ascending order.
To verify how your actual data looks like, add the second line below to your code. Then run your chart in Google Chrome with Console open (F12 then select Console tab)
chart.dataProvider = data;
console.debug(data);
Check the datapoints for irregularities. Especially the last two ones.
The problem was that each chart was rendered within it's own AmCharts.ready method. The documentation states that it's allowed, however it didn't work for me, therefore I wrapped all rendering within one ready method and the issue was fixed.

Categories