0

I need Define Div(chartContainer1) value in a JavaScript "Var" So that it can be defined in JavaScript Chart Unfortunately am unable to do

HTML:code

 <div id="chartContainer" style="height: 300px; width: 100%;">

    <div id="chartContainer1">[
    {y: 10, legendText:"Wii U", label: "Wii U 10%"}, 
    {y: 13, legendText:"3DS", label: "3DS 13%"}, 
    {y: 18, legendText:"PS3", label: "PS3 18%"}, 
    {y: 20, legendText:"Xbox One", label: "Xbox One 20%"}
];</div>

Chart Code: I have tried With

var dsp = document.getElementById("mySpan").innerHTML;  
var dps = document.getElementById("chartContainer1").innerText;

dataPoints is the place Where i need to define text of Div

  var chart = new CanvasJS.Chart("chartContainer",{
    title :{
        text: "Test title"
    },

    data: [{
      type: "stackedBar100",
        dataPoints : dps,
        showInLegend: true, 
        toolTipContent:"{label}"
    }],

  });

  chart.render();
Shaik
  • 930
  • 1
  • 20
  • 48

2 Answers2

0

You could use eval to turn it into an object

var dps = eval(document.getElementById("chartContainer1").innerText);

But be warned about using eval Why is using the JavaScript eval function a bad idea?

Community
  • 1
  • 1
George
  • 6,630
  • 2
  • 29
  • 36
  • `eval` should be **extremely avoided**. By your approach, shouldn't it better be `JSON.parse(document.getElementById("chartContainer1").innerText);` ? – TaoPR Jun 05 '15 at 15:05
  • @TaoP.R. I agree `eval` should be avoided. The only problem here is that it's not JSON data he/she is trying to get it's a JS object – George Jun 05 '15 at 15:19
  • Parsed JSON string is a JSON object, isn't it? You can access `dps[0].legendText`, `dps[0].y` after get parsed, so on – TaoPR Jun 05 '15 at 15:29
  • It's not a JSON string, maybe you misunderstood when I say JS object I mean a [JavaScript Object](http://www.w3schools.com/js/js_objects.asp) which although similar is structured different to a [JSON Object](http://www.w3schools.com/json/), notice how with a JS Object the object's properties are not encased in quotes. – George Jun 05 '15 at 15:31
0

It is not a good practice storing the data inside a DIV that way, because that JSON data will get displayed on the screen before the chart is rendered.

Please read : https://developer.mozilla.org/en-US/docs/Web/Guide/HTML/Using_data_attributes

Instead, if you really want to attach the data along with a particular HTML element, you should store it in a data-something attribute like this:

<div id="chartContainer1" data-content='[
    {"y": 10, "legendText":"Wii U", "label": "Wii U 10%"}, 
    {"y": 13, "legendText":"3DS", "label": "3DS 13%"}, 
    {"y": 18, "legendText":"PS3", "label": "PS3 18%"}, 
    {"y": 20, "legendText":"Xbox One", "label": "Xbox One 20%"}
]'></div>

And fetch the data from the attribute for render by:

var dps = JSON.parse(document.getElementById("chartContainer1").dataset.content);

Or classically,

var dps = JSON.parse(document.getElementById("chartContainer1").getAttribute("data-content"));
TaoPR
  • 5,932
  • 3
  • 25
  • 35