AmCharts 4 - column's bullet/babel position - amcharts4
everyone
So, I'm having some trouble with AmCharts 4 again.
Is there any way to always show the column's bullet/label?
My case
We can use this example, from AmCharts documentation, to reproduce my situation. Just set labelBullet.label.dy to 20 positive.
labelBullet.label.dy = 20;
https://codepen.io/team/amcharts/pen/VxbVeq
Thanks.
You can set maskBullets to false on the chart instance to prevent it from clipping the LabelBullet, e.g. chart.maskBullets = false;
Demo:
// Create chart instance
var chart = am4core.create("chartdiv", am4charts.XYChart);
// Add data
chart.data = [{
"date": new Date(2018, 3, 20),
"value": 90
}, {
"date": new Date(2018, 3, 21),
"value": 102
}, {
"date": new Date(2018, 3, 22),
"value": 65
}, {
"date": new Date(2018, 3, 23),
"value": 62
}, {
"date": new Date(2018, 3, 24),
"value": 55
}, {
"date": new Date(2018, 3, 25),
"value": 81
}];
chart.maskBullets = false;
// Create axes
var dateAxis = chart.xAxes.push(new am4charts.DateAxis());
// Create value axis
var valueAxis = chart.yAxes.push(new am4charts.ValueAxis());
// Create series
var lineSeries = chart.series.push(new am4charts.LineSeries());
lineSeries.dataFields.valueY = "value";
lineSeries.dataFields.dateX = "date";
lineSeries.name = "Sales";
lineSeries.strokeWidth = 3;
// Add simple bullet
var circleBullet = lineSeries.bullets.push(new am4charts.CircleBullet());
circleBullet.circle.stroke = am4core.color("#fff");
circleBullet.circle.strokeWidth = 2;
var labelBullet = lineSeries.bullets.push(new am4charts.LabelBullet());
labelBullet.label.text = "{value}";
labelBullet.label.dy = 20;
<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: 100%; height: 300px;"></div>
Related
Line chart out of axis boundary after extended the chart draw function
I referred to "How to change line segment color of a line graph in Chart.js" and extended the chart to redraw the line segment with a different color. But after adding plugin "chartjs-plugin-zoom" to support zoom in/out the chart, I could see the line chart is out of axis boundary. See the following code. https://jsfiddle.net/sd9rx84g/2/ var ctx = document.getElementById('myChart').getContext('2d'); //adding custom chart type Chart.defaults.multicolorLine = Chart.defaults.line; Chart.controllers.multicolorLine = Chart.controllers.line.extend({ draw: function(ease) { var startIndex = 0, meta = this.getMeta(), points = meta.data || [], colors = this.getDataset().colors, area = this.chart.chartArea, originalDatasets = meta.dataset._children .filter(function(data) { return !isNaN(data._view.y); }); function _setColor(newColor, meta) { meta.dataset._view.borderColor = newColor; } if (!colors) { Chart.controllers.line.prototype.draw.call(this, ease); return; } for (var i = 2; i <= colors.length; i++) { if (colors[i-1] !== colors[i]) { _setColor(colors[i-1], meta); meta.dataset._children = originalDatasets.slice(startIndex, i); meta.dataset.draw(); startIndex = i - 1; } } meta.dataset._children = originalDatasets.slice(startIndex); meta.dataset.draw(); meta.dataset._children = originalDatasets; points.forEach(function(point) { point.draw(area); }); } }); var chart = new Chart(ctx, { // The type of chart we want to create type: 'multicolorLine', // The data for our dataset data: { labels: ["1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12"], datasets: [{ label: "My First dataset", borderColor: 'rgb(255, 99, 132)', data: [35, 10, 5, 2, 20, 30, 45, 0, 10, 5, 2, 20, 30, 45], //first color is not important colors: ['', 'red', 'green', 'blue', 'red', 'brown', 'black'] }] }, // Configuration options go here options: { plugins: { zoom: { pan: { // pan options and/or events enabled: true, mode: 'xy' }, zoom: { enabled: true, drag: false, mode: 'x', speed: 1 } } } } }); <script src="https://cdn.jsdelivr.net/npm/chart.js#2.9.3"></script> <script src="https://cdn.jsdelivr.net/npm/hammerjs#2.0.8"></script> <script src="https://cdn.jsdelivr.net/npm/chartjs-plugin-zoom#0.7.7"></script> <canvas id="myChart"></canvas> line chart out of axis boundary How can I resolve this issue? Thanks Forrest
Adding string pluralization to amCharts tooltip
I'm using an amCharts v4 Pie chart and currently I have this tooltip that appears when hovering on the slices: series.slices.template.tooltipText = "{category}: {value.percent.formatNumber('#.')}% ({value} hours) However, I would like to have the "hours" pluralized correctly, i.e. when {value} equals 1, I would like to have the text hour instead of hours. Is something like this possible? I've found the Adapters, but I don't think they will be usable here because I'm using a special format with category, percentage and the text.
Adapters are the perfect use case for this. You can look at the dataItem associated with the tooltip and use that logic to determine whether return the pluralized version of your tooltip or use the default one, for example: pieSeries.slices.template.adapter.add('tooltipText', function(tooltipText, target) { if (target.dataItem && target.dataItem.value == 1) { return tooltipText.replace('hours', 'hour') } else { return tooltipText; } }); Demo below: var chart = am4core.create("chartdiv", am4charts.PieChart); chart.data = [{ "country": "Lithuania", "hours": 1 }, { "country": "Czechia", "hours": 2 }, { "country": "Ireland", "hours": 3 }, { "country": "Germany", "hours": 1 }, { "country": "Australia", "hours": 1 }, { "country": "Austria", "hours": 1 }]; var pieSeries = chart.series.push(new am4charts.PieSeries()); pieSeries.dataFields.value = "hours"; pieSeries.dataFields.category = "country"; pieSeries.slices.template.tooltipText = "{category}: {value.percent.numberFormatter('#.')}% ({value} hours)"; pieSeries.slices.template.adapter.add('tooltipText', function(tooltipText, target) { if (target.dataItem && target.dataItem.value == 1) { return tooltipText.replace('hours', 'hour') } else { return tooltipText; } }); chart.legend = new am4charts.Legend(); #chartdiv { width: 100%; height: 400px; } <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>
number of labels on the horizontal axis in Google Chartwrapper
In Google Charts, the 'hAxis': {'gridlines': {'count': 3} } statement seems to work, but when I'm using chartWrapper as part of an interactive plot, it does not. I don't really care about vertical gridlines, but I want to control how many labels are on the X axis. I think labels are usually attached to gridlines - one label per gridline. I have an example from the Google Charts website, where the only thing I changed was to put try and put in 3 gridlines: https://jsfiddle.net/emorris/gLcq1h2j/
chart option ticks is only supported by a continuous axis in the fiddle you shared, the view placed on the chart, converts the first column from type 'date' to 'string', which results in a discrete axis // Convert the first column from 'date' to 'string'. 'view': { 'columns': [{ 'calc': function(dataTable, rowIndex) { return dataTable.getFormattedValue(rowIndex, 0); }, 'type': 'string' }, 1, 2, 3, 4] } to control how many labels are on the X axis, remove the view to build the ticks dynamically here, use the state of the range filter, to know the date range currently displayed on the chart the chart will need to be redrawn when the control's 'statechange' event fires see following working snippet, an axis label is created for every 5 days... google.charts.load('current', { callback: drawChartRangeFilter, packages: ['corechart', 'controls'] }); function drawChartRangeFilter() { var data = new google.visualization.DataTable(); data.addColumn('date', 'Date'); data.addColumn('number', 'Stock low'); data.addColumn('number', 'Stock open'); data.addColumn('number', 'Stock close'); data.addColumn('number', 'Stock high'); var open, close = 300; var low, high; for (var day = 1; day < 121; ++day) { var change = (Math.sin(day / 2.5 + Math.PI) + Math.sin(day / 3) - Math.cos(day * 0.7)) * 150; change = change >= 0 ? change + 10 : change - 10; open = close; close = Math.max(50, open + change); low = Math.min(open, close) - (Math.cos(day * 1.7) + 1) * 15; low = Math.max(0, low); high = Math.max(open, close) + (Math.cos(day * 1.3) + 1) * 15; var date = new Date(2012, 0, day); data.addRow([date, Math.round(low), Math.round(open), Math.round(close), Math.round(high)]); } var dashboard = new google.visualization.Dashboard( document.getElementById('dashboard') ); var control = new google.visualization.ControlWrapper({ controlType: 'ChartRangeFilter', containerId: 'control', options: { filterColumnIndex: 0, ui: { chartType: 'LineChart', chartOptions: { chartArea: { width: '92%' }, hAxis: { baselineColor: 'none' }, height: 72 }, chartView: { columns: [0, 3] }, minRangeSize: 86400000 } }, state: { range: { start: new Date(2012, 1, 9), end: new Date(2012, 2, 20) } } }); var chart = new google.visualization.ChartWrapper({ chartType: 'CandlestickChart', containerId: 'chart', options: { chartArea: { height: '100%', width: '100%', top: 12, left: 48, bottom: 48, right: 48 }, vAxis: { viewWindow: { min: 0, max: 2000 } }, legend: { position: 'none' } } }); google.visualization.events.addListener(control, 'statechange', setAxisTicks); function setAxisTicks() { var oneDay = (1000 * 60 * 60 * 24); var dateRange = control.getState().range; var ticksAxisH = []; for (var i = dateRange.start.getTime(); i <= dateRange.end.getTime(); i = i + (oneDay * 5)) { ticksAxisH.push(new Date(i)); } if (ticksAxisH.length > 0) { ticksAxisH.push(new Date(ticksAxisH[ticksAxisH.length - 1].getTime() + (oneDay * 5))); } chart.setOption('hAxis.ticks', ticksAxisH); if (chart.getDataTable() !== null) { chart.draw(); } } setAxisTicks(); dashboard.bind(control, chart); drawDashboard(); $(window).resize(drawDashboard); function drawDashboard() { dashboard.draw(data); } } <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <script src="https://www.gstatic.com/charts/loader.js"></script> <div id="dashboard"> <div id="chart"></div> <div id="control"></div> </div>
Legend and axis tooltip comes with black box with google chart (Firefox
I am currently working on Google Charts with below Version. google.charts.load('upcoming', {packages: ['corechart']); When i enter a mouse on axis and legend tooltip comes with black box as per attached image. I also tried "Current" and "42" version but still getting a same issue as in attached image. I am facing this issue with Firefox. Google Line Chart- Tooltip with balck box in Firefox Is it a bug in Google Chart API or anything else?
tooltip seems to work fine here any code / css / chart options you can share? see following working snippet... google.charts.load('current', { callback: drawChart, packages: ['corechart'] }); function drawChart() { var dataTable = new google.visualization.DataTable({ cols: [ {id: 'x', label: 'Date', type: 'date'}, {id: 'y', label: 'Fn', type: 'number'}, {id: 'z', label: 'Shade', type: 'number'} ] }); var formatDate = new google.visualization.DateFormat({ pattern: 'MMM-dd-yyyy' }); var oneDay = (1000 * 60 * 60 * 24); var startDate = new Date(2016, 10, 27); var endDate = new Date(); var ticksAxisH = []; var dateRanges = [ {start: new Date(2017, 0, 1), end: new Date(2017, 0, 20)}, {start: new Date(2017, 1, 5), end: new Date(2017, 1, 10)} ]; var maxShade = 200; for (var i = startDate.getTime(); i < endDate.getTime(); i = i + oneDay) { // x = date var rowDate = new Date(i); var xValue = { v: rowDate, f: formatDate.formatValue(rowDate) }; // y = 2x + 8 var yValue = (2 * ((i - startDate.getTime()) / oneDay) + 8); // z = null or max shade var zValue = null; dateRanges.forEach(function (range) { if ((rowDate.getTime() >= range.start.getTime()) && (rowDate.getTime() <= range.end.getTime())) { zValue = maxShade; } }); // add data row dataTable.addRow([ xValue, yValue, zValue ]); // add tick every 7 days if (((i - startDate.getTime()) % 7) === 0) { ticksAxisH.push(xValue); } } var container = document.getElementById('chart_div'); var chart = new google.visualization.ChartWrapper({ chartType: 'ComboChart', dataTable: dataTable, options: { chartArea: { bottom: 64, top: 48 }, hAxis: { slantedText: true, ticks: ticksAxisH }, legend: { position: 'top' }, lineWidth: 4, series: { // line 0: { color: '#00acc1' }, // area 1: { areaOpacity: 0.6, color: '#ffe0b2', type: 'area', visibleInLegend: false }, }, seriesType: 'line' } }); chart.draw(container); } <script src="https://www.gstatic.com/charts/loader.js"></script> <div id="chart_div"></div>
Column Chart in Google Charts is displaying bars 10 times proper height
I can't figure out why, I've triple checked that I'm passing in the right values. When I hover over any of the bars it displays the right data, but every single one of them displays at 10x scale on the graph and I can't figure out why. Here's my code if it helps: var dashboard2 = new google.visualization.Dashboard( document.getElementById('dashboard')); var control2 = new google.visualization.ControlWrapper({ 'controlType': 'ChartRangeFilter', 'containerId': 'control2', 'options': { // Filter by the date axis. 'filterColumnIndex': 0, 'ui': { 'chartType': 'LineChart', 'chartOptions': { 'chartArea': {'width': '80%'}, 'hAxis': {'baselineColor': 'none'} }, // Display a single series that shows the closing value of the stock. // Thus, this view has two columns: the date (axis) and the stock value (line series). 'chartView': { 'columns': [0, 1, 14] }, // 1 day in milliseconds = 24 * 60 * 60 * 1000 = 86,400,000 'minRangeSize': 259200000 } }, // Initial range: 2012-02-09 to 2012-03-20. 'state': {'range': {'start': new Date(2012, 11, 7), 'end': new Date()}} }); var chart2 = new google.visualization.ChartWrapper({ 'chartType': 'ComboChart', 'containerId': 'chart2', 'options': { // Use the same chart area width as the control for axis alignment. 'chartArea': {'height': '80%', 'width': '80%'}, 'hAxis': {'slantedText': false}, 'vAxis': {'viewWindow': {'min': 0, 'max': 400}}, 'title': 'Sales Made by Affiliate Name', 'seriesType': "bars", 'series': {0: {type: "line"}, 13: {type: "line"}}, 'isStacked': true }, // Convert the first column from 'date' to 'string'. 'view': { 'columns': [ { 'calc': function(dataTable, rowIndex) { return dataTable.getFormattedValue(rowIndex, 0); }, 'type': 'string' }, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14] } }); var jsonData2 = $.ajax({ url: "getData.php", dataType:"json", async: false }).responseText; // Create our data table out of JSON data loaded from server var data2 = new google.visualization.DataTable(jsonData2); dashboard2.bind(control2, chart2); dashboard2.draw(data2); Edit: Here's a small bit of the data at the very beginning, because I don't want to give out our data, but I suppose it might be necessary to get an idea for what is being passed in. I cut out the starting bracket for readability: "cols":[ {"id":"","label":"Date","pattern":"","type":"date"}, {"id":"","label":"Total","pattern":"","type":"number"}, {"id":"","label":"andersce99","pattern":"","type":"number"}, {"id":"","label":"sojourn","pattern":"","type":"number"}, {"id":"","label":"warriorplus","pattern":"","type":"number"}, {"id":"","label":"potpie queen","pattern":"","type":"number"}, {"id":"","label":"60minuteaffiliate","pattern":"","type":"number"}, {"id":"","label":"bob voges","pattern":"","type":"number"}, {"id":"","label":"Grayth","pattern":"","type":"number"}, {"id":"","label":"TiffanyDow","pattern":"","type":"number"}, {"id":"","label":"AmandaT","pattern":"","type":"number"}, {"id":"","label":"Gaz Cooper","pattern":"","type":"number"}, {"id":"","label":"Sam England","pattern":"","type":"number"}, {"id":"","label":"Matthew Olson","pattern":"","type":"number"}, {"id":"","label":"Average Per Day Over Time","pattern":"","type":"number"} ], "rows": [ {"c":[{"v":"Date(2012,11,7)","f":null},{"v":"387","f":null},{"v":"19","f":null},{"v":"275","f":null},{"v":"8","f":null},{"v":"0","f":null},{"v":"35","f":null},{"v":"3","f":null},{"v":"21","f":null},{"v":"0","f":null},{"v":"0","f":null},{"v":"0","f":null},{"v":"11","f":null},{"v":"6","f":null},{"v":"387","f":null}]}, {"c":[{"v":"Date(2012,11,8)","f":null},{"v":"98","f":null},{"v":"11","f":null},{"v":"39","f":null},{"v":"1","f":null},{"v":"0","f":null},{"v":"15","f":null},{"v":"0","f":null},{"v":"7","f":null},{"v":"9","f":null},{"v":"0","f":null},{"v":"0","f":null},{"v":"3","f":null},{"v":"6","f":null},{"v":"242.5","f":null}]}, {"c":[{"v":"Date(2012,11,9)","f":null},{"v":"58","f":null},{"v":"7","f":null},{"v":"16","f":null},{"v":"0","f":null},{"v":"0","f":null},{"v":"4","f":null},{"v":"0","f":null},{"v":"3","f":null},{"v":"10","f":null},{"v":"2","f":null},{"v":"9","f":null},{"v":"0","f":null},{"v":"2","f":null},{"v":"181","f":null}]}, {"c":[{"v":"Date(2012,11,10)","f":null},{"v":"196","f":null},{"v":"5","f":null},{"v":"8","f":null},{"v":"126","f":null},{"v":"0","f":null},{"v":"2","f":null},{"v":"35","f":null},{"v":"0","f":null},{"v":"7","f":null},{"v":"4","f":null},{"v":"3","f":null},{"v":"1","f":null},{"v":"0","f":null},{"v":"184.75","f":null}]}, {"c":[{"v":"Date(2012,11,11)","f":null},{"v":"76","f":null},{"v":"7","f":null},{"v":"5","f":null},{"v":"17","f":null},{"v":"30","f":null},{"v":"7","f":null},{"v":"1","f":null},{"v":"1","f":null},{"v":"2","f":null},{"v":"1","f":null},{"v":"4","f":null},{"v":"0","f":null},{"v":"0","f":null},{"v":"163","f":null}]}, {"c":[{"v":"Date(2012,11,12)","f":null},{"v":"48","f":null},{"v":"4","f":null},{"v":"5","f":null},{"v":"9","f":null},{"v":"20","f":null},{"v":"7","f":null},{"v":"1","f":null},{"v":"0","f":null},{"v":"0","f":null},{"v":"0","f":null},{"v":"0","f":null},{"v":"0","f":null},{"v":"0","f":null},{"v":"143.833333333","f":null}]}, {"c":[{"v":"Date(2012,11,13)","f":null},{"v":"21","f":null},{"v":"3","f":null},{"v":"2","f":null},{"v":"5","f":null},{"v":"4","f":null},{"v":"0","f":null},{"v":"0","f":null},{"v":"1","f":null},{"v":"2","f":null},{"v":"0","f":null},{"v":"1","f":null},{"v":"0","f":null},{"v":"0","f":null},{"v":"126.285714286","f":null}]}, {"c":[{"v":"Date(2012,11,14)","f":null},{"v":"12","f":null},{"v":"1","f":null},{"v":"1","f":null},{"v":"2","f":null},{"v":"4","f":null},{"v":"2","f":null},{"v":"0","f":null},{"v":"0","f":null},{"v":"0","f":null},{"v":"0","f":null},{"v":"0","f":null},{"v":"0","f":null},{"v":"0","f":null},{"v":"112","f":null}]}, {"c":[{"v":"Date(2012,11,15)","f":null},{"v":"8","f":null},{"v":"3","f":null},{"v":"0","f":null},{"v":"1","f":null},{"v":"2","f":null},{"v":"0","f":null},{"v":"0","f":null},{"v":"1","f":null},{"v":"0","f":null},{"v":"1","f":null},{"v":"0","f":null},{"v":"0","f":null},{"v":"0","f":null},{"v":"100.444444444","f":null}]}
Your data is formatted incorrectly: numbers need to be stored as numbers in the JSON, not as strings. As an example, this: {"v":"387","f":null} should be like this: {"v":387,"f":null} If you are using PHP's json_encode function to build the JSON, you can add JSON_NUMERIC_CHECK as an argument to the function call to output the numbers properly: json_encode($myData, JSON_NUMERIC_CHECK);