I am using the ZingChart library to graph results from an API call. When I pass in a normal array for the "values" field of the chart data object, everything works fine. However, when I pass in an array made from Object.keys(titleSet)
(where titleSet
is a normal Javascript object), the graph displays as follows:
Example Chart
As you can see, the x-axis is now labeled with numbers instead of the array of strings. But when I print out the the result of Object.keys(titleSet)
vs. passing in a normal array, they both appear to be the same in the console. Can anyone help me figure out what I'm doing wrong?
//List of movies inputted by the user
var movieList = [];
var movieSet = {};
var IMDBData = {
"values": [],
"text": "IMDB",
};
var metascoreData = {
"values": [],
"text": "Metascore"
};
var RTMData = {
"values": [],
"text": "Rotten Tomatoes Meter"
};
var RTUData = {
"values": [],
"text": "Rotten Tomatoes User"
};
var chartData = {
"type":"bar",
"legend":{
"adjust-layout": true
},
"plotarea": {
"adjust-layout":true
},
"plot":{
"stacked": true,
"border-radius": "1px",
"tooltip": {
"text": "Rated %v by %plot-text"
},
"animation":{
"effect":"11",
"method":"3",
"sequence":"ANIMATION_BY_PLOT_AND_NODE",
"speed":10
}
},
"scale-x": {
"label":{ /* Scale Title */
"text":"Movie Title",
},
"values": Object.keys(movieSet) /* Needs to be list of movie titles */
},
"scale-y": {
"label":{ /* Scale Title */
"text":"Total Score",
}
},
"series":[metascoreData, IMDBData, RTUData, RTMData]
};
var callback = function(data)
{
var resp = JSON.parse(data);
movieSet[resp.Title] = true;
//Render
zingchart.render({
id:'chartDiv',
data:chartData,
});
};
Axis label, specified as a string scalar, character vector, string array, character array, cell array, categorical array, or numeric value. To include numeric variables with text in a label, use the num2str function. For example: x = 42; txt = ['The value is ',num2str(x)];
To properly label a graph, you should identify which variable the x-axis and y-axis each represent. Don't forget to include units of measure (called scale) so readers can understand each quantity represented by those axes. Finally, add a title to the graph, usually in the form "y-axis variable vs. x-axis variable."
Axis labels are text that mark major divisions on a chart. Category axis labels show category names; value axis labels show values.
Display text labels in X-axis of scatter chart Actually, there is no way that can display text labels in the X-axis of scatter chart in Excel, but we can create a line chart and make it look like a scatter chart. 1. Select the data you use, and click Insert > Insert Line & Area Chart > Line with Markers to select a line chart.
We first need to create a new X and Y axis, that will be added to the existing chart. The X-axis will have the numbers from 1 to 5 and Y will have five zeroes. We will first add our X-axis by selecting the range J2:J6, then clicking on CTRL + C to copy it, then click on our chart and click CTRL+P to paste our selection.
ZingChart currently supports linear, category, and time-series scales on the x-axis. Use the scaleX object to configure your scale. Refer to the X/Y-Axis Scale Configuration Tutorial for more information. Example structure of scale object. Sets the transparency level of the object.
When we say that, we do not only mean numerical representation but graphical as well. One of the things that can often bother people and which is not easily achieved is to show labels instead of numbers on the x-axis. We will show how to do this on the example below:
Full Disclosure, I'm a member of the ZingChart team.
Thank you for updating your question. The problem is you have defined your variable movieSet
before the variablechartData
. When parsing the page, top down, it is executing Object.keys({}) on an empty object when creating the variable chartData
. You should just directly assign it into your config later on chartData['scale-x']['values'] = Object.keys(moviSet)
.
var callback = function(data)
{
var resp = JSON.parse(data);
movieSet[resp.Title] = true;
//Render
zingchart.render({
id:'chartDiv',
data:chartData,
});
};
There is a problem with the above code as well. It seems you are calling render on the chart every time you call this API. You should have one initial zingchart.render()
and then from there on out use our API. I would suggest setdata
method as it replaces a whole new JSON packet or modify
method.
I am making some assumptions on how you are handling data. Regardless, check out the following demo
var movieValues = {};
var myConfig = {
type: "bar",
scaleX:{
values:[]
},
series : [
{
values : [35,42,67,89,25,34,67,85]
}
]
};
zingchart.render({
id : 'myChart',
data : myConfig,
height: 300,
width: '100%'
});
var callback = function(data) {
movieValues[data.title] = true;
myConfig.scaleX.values = Object.keys(movieValues);
zingchart.exec('myChart', 'setdata', {
data:myConfig
})
}
var index = 0;
var movieNamesFromDB = ['Sausage Party', 'Animal House', 'Hot Rod', 'Blazing Saddles'];
setInterval(function() {
if (index < 4) {
callback({title:movieNamesFromDB[index++]});
}
},1000)
<!DOCTYPE html>
<html>
<head>
<!--Assets will be injected here on compile. Use the assets button above-->
<script src= "https://cdn.zingchart.com/zingchart.min.js"></script>
<script> zingchart.MODULESDIR = "https://cdn.zingchart.com/modules/";
</script>
<!--Inject End-->
</head>
<body>
<div id='myChart'></div>
</body>
</html>
If you noticed in the demo, the length of scaleX.values
determines how many nodes are shown on the graph. If you change values
to labels
this wont happen.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With