Plotlyjs ticktext/tickvals not showing xaxis labels - javascript
I have been trying to utilize the tickvals/ticktext on plotlyjs to be able to show duplicate values on a graph for the xaxis. It works in the aspect of separating the data (having multiple duplicate values be on the xaxis, but a 1:1 data point) -- the issue that I am having is that no xaxis tick marks or xaxis labels show up for the ticks.
Here is a code pen of my problem:
https://codepen.io/hiquetj/pen/yLeBNjJ
Here is the code for easier reading:
var trace1 = {
y: [1323,1070,849,1312,1214,1264,680,2006,1625,0,1199,1770,1880,708,603,649,1384,732,2312,92,1377,1125,1219,1757,1207,2397,1190,1818,2846,1200,1685,1563,1203,1059,820,4303,812,3640,1924,4185,1269,2263,1140,1210,1638,1362,4915,1594,683,1222],
mode: 'markers'
};
var trace2 = {
y: [5870,1574,1372,2093,1361,1388,615,1927,947,,1040,,,638,546,542,1051,637,4161,289,1103,0,1308,1658,1340,2198,1245,1568,2392,1297,1583,1281,1282,697,482,3633,653,3233,2096,3815,1401,2116,867,1232,1420,1010,4091,478,208,1262],
mode: 'lines'
};
var trace3 = {
y: [1141,1324,424,668,414,407,494,1763,1100,,522,360,108,474,145,621,689,234,154,40,303,364,407,1133,347,1527,358,1745,1223,345,1131,1024,365,754,614,1894,287,5332,363,568,335,1545,1294,383,1255,1107,2090,612,66,390],
mode: 'lines+markers'
};
var data = [ trace1, trace2, trace3 ];
var layout = {
xaxis: {
ticktext: [24346247,24346247,24346247,24346247,24332892,24332892,24332892,24332670,24332670,24330025,24330025,24330025,24330025,24330025,24330025,24330025,24330025,24330025,24330025,24330025,24330025,24330025,24330025,24330025,24330025,24330025,24330025,24330025,24316962,24316962,24316962,24290013,24290013,24290013,24290013,24290013,23738933,23738933,23738933,23738933,23738933,23738933,23738933,23738933,23738933,23738933,23738933,23738933,23738933,23738933],
tickvals: [24346247,24346247,24346247,24346247,24332892,24332892,24332892,24332670,24332670,24330025,24330025,24330025,24330025,24330025,24330025,24330025,24330025,24330025,24330025,24330025,24330025,24330025,24330025,24330025,24330025,24330025,24330025,24330025,24316962,24316962,24316962,24290013,24290013,24290013,24290013,24290013,23738933,23738933,23738933,23738933,23738933,23738933,23738933,23738933,23738933,23738933,23738933,23738933,23738933,23738933],
},
};
Plotly.newPlot('myDiv', data, layout, {showSendToCloud: true});
Example of x axis labels not showing up through codepen.
If you do not set x values for your traces, they are automatically assigned x values from 0 to n. Your ticktexts will therefore only be visible if your tickval array is [0, 1, 2, 3, ..., n], see this jsFiddle: https://jsfiddle.net/9okdx4b2/
Related
Apache Echarts: dataZoom's miniature misrepresents data in a managed plot (the plot drawn over the slider doesn't match the main plot's data)
When I create simple line plot/chart using Apache Echarts I also can add built-in data scaling mechanism: dataZoom. It reaches its main goal, but there is a question to scaled data representation, made by dataZoom. By default, dataZoom doesn't take into account the chart scale limits ticks or/and the minimum and maximum allowable values (range of a function, represented by the plot). Instead, the thumbnail of the chart is drawn on the specific value range passed to the plot in series section. In addition, everytime a small indent is added from the minimum and maximum values to the borders of the graphic element. As a result, the representation of the visualised data looks inconsistent with reality: null is not null, max is not max (because they don't match the lower and higher bounds of the coordinate area of the thumbnail plot, respectively), the amplitude of the chart fluctuations does not correspond to the scale of real data fluctuations. Screenshot Is there a way (documented or undocumented) to remove the indents and force the plot to use the minimum and maximum values allowed for the yAxis ticks? I drawn a small example, it may be pasted to Echarts online editor. let x = []; let y = []; let scaled = []; /*y = [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 300, 300, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 0, 0, 0, 0, 0, 0 ];*/ for (let i = 1; i < 300; i++) { x.push(i); element = Math.random() * 40 + 50; y.push(element); scaled.push(element * 6 - 250); } option = { xAxis: { data: x }, yAxis: { min: 0, max: 300 }, dataZoom: [ { start: 50, end: 58.5 } ], series: [ { name: 'Fake Data', type: 'line', symbol: 'none', data: y }, { name: 'Simulated Scaling', type: 'line', symbol: 'none', lineStyle: { opacity: 0.3 }, data: scaled } ] }; As you can see, the magnitude of the fluctuations of the graph, drawn by dataZoom doesn't correspond rather to the main data, but to some kind of artificial transformation of them (light green graph). Then try to comment 11st line and uncomment lines from 4 to 7. At the start of the plot you'll see main graph touching y zero line, but not on the thumbnail. I didn't find any params for dataZoom that make them to look like expected.
Highcharts: Can I animate changing the order of bars on bar chart?
I want to create a chart like the below. https://www.reddit.com/r/interestingasfuck/comments/9togwf/the_major_world_economies_over_time/ I created this chart by Highcharts. But, I can't animate changing the order of bars. function rotate(array, times) { while (times--) { var temp = array.shift(); array.push(temp) } } window.data = { categories: ['Africa', 'America', 'Asia', 'Europe', 'Oceania'], y_values: [100, 200, 300, 400, 500], colors: ['#DC4D3A', '#E93D3F', '#83C6C7', '#46D388', '#D1D785'] }; document.getElementById("button").addEventListener("click", function () { for (var i = 0; i < data['y_values'].length; i++) { chart.series[0].data[i].update({y: data['y_values'][i]}); chart.series[0].data[i].update({color: data['colors'][i]}); } chart.xAxis[0].update({categories: data['categories']}); rotate(data['y_values'], 1); rotate(data['categories'], 1); rotate(data['colors'], 1); }, false); All code I wrote are in JSFiddle. https://jsfiddle.net/Shinohara/35e8gbyz/ Please anyone can help me?
Highcharts provides animate method for SVG elements, which you can use to achieve the wanted result. You need to animate columns, axis labels and data labels: document.getElementById("button").addEventListener("click", function() { var points = chart.series[0].points, ticks = chart.xAxis[0].ticks; points[0].graphic.animate({ x: points[1].shapeArgs.x }); points[1].graphic.animate({ x: points[0].shapeArgs.x }); points[0].dataLabel.animate({ y: points[1].dataLabel.translateY }); points[1].dataLabel.animate({ y: points[0].dataLabel.translateY }); ticks[0].label.animate({ y: ticks[1].label.xy.y }); ticks[1].label.animate({ y: ticks[0].label.xy.y }); }, false); Live demo: https://jsfiddle.net/BlackLabel/ux59vcd6/ API Reference: https://api.highcharts.com/class-reference/Highcharts.SVGElement#animate
It seems there is no native implementation. The only idea I have it to not display Y axis labels at all. Using SVGRenderer draw the text and save it as a variable (for further updating) chart.customText = chart.renderer.text('label name', 100, 100) // label name, X, Y .attr({ useHTML: true }) .css({ fontSize: '16px' // and other CSS if necessary }) .add(); Then you can update x, y or text chart.customText.attr({ str: 'new text. You can use HTML', y: 150 // new value }) Your next step is to do something with Y position. Or even you can try to draw only 1 text with HTML that contains all labels. Then using JS move them as you need.
Trying to use HighCharts to build solidgauge chart with multiple layer
I'm trying to build some chart like this one: Chart Visual But the main struggle is to add two different series that complement each other. I really appreciate any help that someone could give me. Many thanks in advance.
You can achieve it, but the process of implementation is not so easy. I prepared the example which shows how to do that, and I will try to explain what I did, step by step. First, you need to define your data array just like that: var data = [40, 30, 10, 20] Then define your chart configuration, and inside of chart.events.load function handler put whole logic of creating desired effect. Next step is iterate on all data positions, and create its own specific point, series, yAxis and pane, basing on calculations like below: load() { var chart = this, series = [], panes = [], yAxes = [], radius = 112, innerRadius = 88, pointAngle, prevPointAngle = 0, pointPadding = (radius - innerRadius) / 4, colors = Highcharts.getOptions().colors, additionalPointPadding = 2; data.forEach(function(p, i) { pointAngle = (p * 360) / 100 // Calculate point angle // Prepare pane for each point panes.push({ startAngle: prevPointAngle + pointPadding + additionalPointPadding, endAngle: (pointAngle + prevPointAngle) - pointPadding - additionalPointPadding, background: [{ backgroundColor: '#fff', borderWidth: 0 }] }) // Prepare yAxis for specific pane yAxes.push({ min: 0, max: 100, lineWidth: 0, tickPositions: [], pane: i }) // Prepare series with specific point series.push({ name: 'Exercise ' + i, data: [{ color: colors[i], radius: radius + '%', innerRadius: innerRadius + '%', y: 100, percents: p }], yAxis: i }) prevPointAngle += pointAngle }) And finally, update our chart by new objects: chart.update({ pane: panes, yAxis: yAxes, series: series },true, true) Last thing you have to know, that your chart configuration should have the same amount of empty objects in pane array, like the data positions, e.g: var data = [10, 80, 10] (...) pane: [{},{},{}] Here is the example which shows the final effect: https://jsfiddle.net/yamu5z9r/ Kind regards!
Remove axis on IDD chart
I'm trying to visualize a time series using IDD. How can I remove the X axis that indicates float values from the chart and leave only time stamps? Here is my code: var timeSeriesChart = InteractiveDataDisplay.asPlot("chart"); var timeSeriesData = JSON.parse('{\ "times":["2016-03-28 16:00","2016-03-28 17:00","2016-03-28 18:00"],\ "time_locations":[0.6,1.2,1.8],\ "values":[3.0, 4.0, 5.0]}'); timeSeriesChart.polyline("Time series", { y: timeSeriesData.values, x: false, stroke: "rgb(89,150,255)", thickness: 3 }); timeSeriesChart.addAxis("bottom", "labels", { labels: timeSeriesData.times, ticks: timeSeriesData.time_locations }); see the chart
Try var numAxis = timeSeriesChart.getAxes("bottom"); numAxis[0].remove(); To remove existing axis before adding your labeled axis. And after you add your labeled axis, attach grid lines to this new axis: var gridLines = $('#chart > div[data-idd-plot="grid"]'); var grid = timeSeriesChart.get(gridLines[0]); grid.xAxis = timeAxis.axis;
C3.js bar plot with dates as x axis
I'm trying to make a C3 bar plot using the X with dates. I have the data formatted already in consecutive dates aggregated by days. I've created a fiddle with what I thought will work: https://jsfiddle.net/8aL1stcs/ the plot as such is working fine, here is a version of the fiddle with the X axis commented out: https://jsfiddle.net/8aL1stcs/1/ var elementID = "#myPlot"; var myData = {}; //myData.x = 'x'; //myData.xFormat = "%Y-%m-%d"; myData.type = 'bar'; myX = ["2015-11-20", "2015-11-21", "2015-11-22","2015-11-23", "2015-11-24"]; myY = [1,2,3,4,5]; myX.splice(0,0,'x'); myY.splice(0,0,'New Reports'); myData.columns = []; //myData.columns.push(myX); myData.columns.push(myY); var chart = c3.generate({ bindto: elementID, data: myData, size: { height: 480, width:400, }, bar: { width: { ratio: 0.5 // this makes bar width 50% of length between ticks } // or //width: 100 // this makes bar width 100px } }); Basically I want to mimic this plot here: For this, I think I need to use some automated way of handling the x-ticks. In other words: I don't want to set the ticks manually.
You missed this in your c3 generate json. axis: { x: { type: 'timeseries', tick: { format: "%b-%d"//format in which you want the output } }, } Working code here Hope this helps!