Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ZingChart X-axis labels showing as numbers instead of strings

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,
  });
};
like image 778
pomtree Avatar asked Aug 14 '16 03:08

pomtree


People also ask

What are the labels at X axis?

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)];

How do you label the axes in a graph?

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."

What does it mean to label an axis?

Axis labels are text that mark major divisions on a chart. Category axis labels show category names; value axis labels show values.

How to display text labels in x-axis of scatter chart in Excel?

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.

How to add the X and y axis to an existing 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.

How do I set the scale on the x-axis in zingchart?

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.

Can We show labels instead of numbers on the x-axis?

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:


1 Answers

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.

like image 141
nardecky Avatar answered Sep 30 '22 06:09

nardecky