I am trying to draw Average, High and Low value straight line on a dimple js bar chart. I have no clue how they can be drawn on y-axis (cost) as straight line that will cut through the bars. Here is the fiddle that has high, low and average values saved into corresponding variable that needed to be drawnon the chart. Any solution? jsfiddle link: http://jsfiddle.net/Ra2xS/14/
var dim = {"width":590,"height":450}; //chart container width
var data = [{"date":"01-02-2010","cost":"11.415679194952766"},{"date":"01-03-2010","cost":"10.81875691467018"},{"date":"01-04-2010","cost":"12.710197879070897"}];
//y- axis (cost) values to plot as straight line over bar chart in different colours
var avg = "11.65";
var high= "12.71";
var low = "10.82";
function barplot(id,dim,data)
{
keys = Object.keys(data[0]);
var xcord = keys[0];
var ycord = keys[1];
var svg = dimple.newSvg(id, dim.width, dim.height);
var myChart = new dimple.chart(svg,data);
myChart.setBounds(60, 30, 505, 305);
var x = myChart.addCategoryAxis("x", xcord);
x.addOrderRule(xcord);
x.showGridlines = true;
var y = myChart.addMeasureAxis("y", ycord);
y.showGridlines = true;
y.tickFormat = ',.1f';
var s = myChart.addSeries(null, dimple.plot.bar);
var s1 = myChart.addSeries(null, dimple.plot.line);
s1.lineWeight = 3;
s1.lineMarkers = true;
myChart.draw(1500);
}
barplot("body",dim,data);
You can do it by adding a separate series with it's own data, unfortunately there's a bug with multiple line series in version 1.1.5 which means line markers go haywire (so I've removed them from the code below). The good news is I've just finished rewriting all the line chart code and this problem will be fixed in the next version (coming in a week or so), also the line will animate properly by rising from the x axis instead of fading in from black.
var dim = {"width":590,"height":450}; //chart container width
var data = [{"date":"01-02-2010","cost":"11.415679194952766"},{"date":"01-03-2010","cost":"10.81875691467018"},{"date":"01-04-2010","cost":"12.710197879070897"}];
function barplot(id,dim,data)
{
keys = Object.keys(data[0]);
var xcord = keys[0];
var ycord = keys[1];
var svg = dimple.newSvg(id, dim.width, dim.height);
var parser = d3.time.format("%d-%m-%Y")
var dateReader = function (d) { return parser.parse(d[xcord]); }
var start = d3.time.month.offset(d3.min(data, dateReader), -1);
var end = d3.time.month.offset(d3.max(data, dateReader), 1);
var myChart = new dimple.chart(svg,data);
myChart.setBounds(60, 30, 505, 305);
//var x = myChart.addCategoryAxis("x", xcord);
var x = myChart.addTimeAxis("x", xcord, "%d-%m-%Y","%b %Y");
x.overrideMin = start;
x.overrideMax = end;
x.addOrderRule(xcord);
x.showGridlines = true;
x.timePeriod = d3.time.months;
x.floatingBarWidth = 100;
var y = myChart.addMeasureAxis("y", ycord);
y.showGridlines = true;
y.tickFormat = ',.1f';
var s1 = myChart.addSeries(null, dimple.plot.bar);
var s2 = myChart.addSeries(null, dimple.plot.line);
s2.lineWeight = 3;
var s3 = myChart.addSeries("Price Break", dimple.plot.line);
s3.data = [
{ "Price Break" : "high", "cost" : 12.71, "date" : parser(start) },
{ "Price Break" : "high", "cost" : 12.71, "date" : parser(end) },
{ "Price Break" : "avg", "cost" : 11.65, "date" : parser(start) },
{ "Price Break" : "avg", "cost" : 11.65, "date" : parser(end) },
{ "Price Break" : "low", "cost" : 10.82, "date" : parser(start) },
{ "Price Break" : "low", "cost" : 10.82, "date" : parser(end) }
];
myChart.draw(1500);
}
barplot("body",dim,data);
http://jsfiddle.net/Ra2xS/28/
I try to make multiple line using amcharts. The type I'm gonna use is smoothed lines (view demo here). I've seen it's possible to draw multiple line charts and multiple value axis on this link. So, I try to implement it on smoothed lines using:
a. 2 lines
b. Both of them are different in scale, so I need two Y axis.
The problem is: only one Y axis appears.
Here's the code I wrote using javascript:
<script type="text/javascript">
var chart;
var graph;
var chartData = [
{"year":"1950", "v1":0.2, "v2":-26},
{"year":"1951", "v1":-0.2, "v2":-45},
{"year":"1952", "v1":1, "v2":-14},
{"year":"1953", "v1":0.2, "v2":6},
{"year":"1954", "v1":-0.5, "v2":18},
{"year":"1955", "v1":0.8, "v2":-8},
{"year":"1956", "v1":-0.4, "v2":-35},
{"year":"1957", "v1":-0.4, "v2":-37},
{"year":"1958", "v1":-0.2, "v2":27},
{"year":"1959", "v1":1.3, "v2":-14},
{"year":"1960", "v1":1, "v2":8},
{"year":"1961", "v1":0, "v2":-6},
{"year":"1962", "v1":0.4, "v2":9},
{"year":"1963", "v1":-0.3, "v2":-38},
{"year":"1964", "v1":0.7, "v2":-3},
{"year":"1965", "v1":0, "v2":-10},
{"year":"1966", "v1":0.7, "v2":-48},
{"year":"1967", "v1":0.2, "v2":22},
{"year":"1968", "v1":1.1, "v2":-22},
{"year":"1969", "v1":-0.2, "v2":-49},
{"year":"1970", "v1":1.5, "v2":-2},
{"year":"1971", "v1":-0.3, "v2":-37},
{"year":"1972", "v1":1, "v2":-44},
{"year":"1973", "v1":-0.4, "v2":18},
{"year":"1974", "v1":1.2, "v2":-17},
{"year":"1975", "v1":-0.3, "v2":-33},
{"year":"1976", "v1":0.6, "v2":-45},
{"year":"1977", "v1":-0.2, "v2":2},
{"year":"1978", "v1":0.5, "v2":18},
{"year":"1979", "v1":-0.3, "v2":-24},
{"year":"1980", "v1":0.5, "v2":14},
{"year":"1981", "v1":-0.5, "v2":-28},
{"year":"1982", "v1":0.7, "v2":-6},
{"year":"1983", "v1":1.1, "v2":-1},
{"year":"1984", "v1":0.9, "v2":-26},
{"year":"1985", "v1":0.8, "v2":0},
{"year":"1986", "v1":1.5, "v2":-27},
{"year":"1987", "v1":1.4, "v2":26},
{"year":"1988", "v1":-0.5, "v2":26},
{"year":"1989", "v1":0.8, "v2":-45},
{"year":"1990", "v1":0.3, "v2":-23},
{"year":"1991", "v1":1.2, "v2":-21},
{"year":"1992", "v1":-0.2, "v2":-31},
{"year":"1993", "v1":0.8, "v2":-12},
{"year":"1994", "v1":0.3, "v2":-35},
{"year":"1995", "v1":1.1, "v2":-1},
{"year":"1996", "v1":0.9, "v2":-14},
{"year":"1997", "v1":-0.1, "v2":11},
{"year":"1998", "v1":1.1, "v2":-29},
{"year":"1999", "v1":-0.2, "v2":11},
{"year":"2000", "v1":-0.4, "v2":-18},
{"year":"2001", "v1":0.6, "v2":-12},
{"year":"2002", "v1":1.5, "v2":-3},
{"year":"2003", "v1":0.6, "v2":-47},
{"year":"2004", "v1":1.5, "v2":-43},
{"year":"2005", "v1":0.1, "v2":-18},
{"year":"2006", "v1":0, "v2":7},
{"year":"2007", "v1":0.5, "v2":-25},
{"year":"2008", "v1":1.5, "v2":-50},
{"year":"2009", "v1":0.4, "v2":28},
{"year":"2010", "v1":0.7, "v2":25},
{"year":"2011", "v1":0.4, "v2":-16},
{"year":"2012", "v1":-0.2, "v2":5},
{"year":"2013", "v1":0.2, "v2":-17},
{"year":"2014", "v1":-0.5, "v2":-22}
];
AmCharts.ready(function () {
// SERIAL CHART
chart = new AmCharts.AmSerialChart();
chart.pathToImages = "../amcharts/images/";
chart.dataProvider = chartData;
chart.marginLeft = 10;
chart.categoryField = "year";
chart.dataDateFormat = "YYYY";
chart.addListener("dataUpdated", zoomChart);
// AXES
// category
var categoryAxis = chart.categoryAxis;
categoryAxis.parseDates = true;
categoryAxis.minPeriod = "YYYY";
categoryAxis.dashLength = 3;
categoryAxis.minorGridEnabled = true;
categoryAxis.minorGridAlpha = 0.1;
// value
var valueAxis = new AmCharts.ValueAxis();
valueAxis.id = "ax1";
valueAxis.axisAlpha = 0;
valueAxis.inside = true;
valueAxis.dashLength = 3;
valueAxis.position = "right";
chart.addValueAxis(valueAxis);
var valueAxis2 = new AmCharts.ValueAxis();
valueAxis2.id = "ax2";
valueAxis2.axisAlpha = 0;
valueAxis2.inside = true;
valueAxis2.dashLength = 3;
valueAxis2.baseValue = -20;
valueAxis2.position = "left";
chart.addValueAxis(valueAxis2);
// GRAPH
graph = new AmCharts.AmGraph();
graph.valueaxis = "ax1";
graph.type = "smoothedLine";
graph.lineColor = "#d1655d";
graph.negativeLineColor = "#637bb6";
graph.bullet = "square";
graph.bulletSize = 8;
graph.bulletBorderColor = "#FFFFFF";
graph.bulletBorderAlpha = 1;
graph.bulletBorderThickness = 2;
graph.lineThickness = 2;
graph.valueField = "v1";
graph.title = "title v1";
graph.balloonText = "[[category]]<br><b><span style='font-size:14px;'>[[value]]</span></b>";
chart.addGraph(graph);
graph2 = new AmCharts.AmGraph();
graph2.valueaxis = "ax2";
graph2.type = "smoothedLine";
graph2.lineColor = "#d1655d";
graph2.negativeLineColor = "#637bb6";
graph2.bullet = "round";
graph2.bulletSize = 8;
graph2.bulletBorderColor = "#FFFFFF";
graph2.bulletBorderAlpha = 1;
graph2.bulletBorderThickness = 2;
graph2.lineThickness = 2;
graph2.valueField = "v2";
graph2.title = "title v2";
graph2.balloonText = "[[category]]<br><b><span style='font-size:14px;'>[[value]]</span></b>";
chart.addGraph(graph2);
// CURSOR
var chartCursor = new AmCharts.ChartCursor();
chartCursor.cursorAlpha = 0;
chartCursor.cursorPosition = "mouse";
chartCursor.categoryBalloonDateFormat = "YYYY";
chart.addChartCursor(chartCursor);
// SCROLLBAR
var chartScrollbar = new AmCharts.ChartScrollbar();
chart.addChartScrollbar(chartScrollbar);
chart.creditsPosition = "bottom-right";
// WRITE
chart.write("chartdiv");
});
Anyone having similar issues? How to overcome this?
It's all very simple, you have a typo:
graph.valueaxis should be graph.valueAxis
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.