1

in the line in the example

d3.json("bullets.json", function(data) {

it is pulling the json data from a local file and passing it to the callback as data to be acted on. How can I change this so that a var I have defined previously is the data source as there doe not seem to be a method in the js to accept a pre-formed data object?

Reason? an embedded web viewer in an application which can dynamically create the html to render, but which has no access to the local file system.

EDIT let me be more specific then.. how can this dataset be fed into the code which generates the bullet chart from the data (when it is stored as an external file) ? it can't be by just going d3.select( etc etc as the sag object needs to be created to hang the title onto

var dataset = {"title":"Sales","subtitle":"£, 000","ranges":[50,200,400],"measures":[100,500],"markers":[250]};

d3.json("bullets.json", function(data) {
 var svg = d3.select("body").selectAll("svg")
  .data(data)
  .enter().append("svg")
john renfrew
  • 393
  • 1
  • 9
  • 30
  • Were that is was that simple.. – john renfrew Jun 22 '15 at 21:40
  • Lars, I have read every example I can find on bullet charts and none of the methods they outline will work as replacements for the d3.json() construct. data=JSON.parse(myJson) then leaving out the line works in the flare example, but not the bullet one. Could you find a way to point to resources where I can learn to solve my problem? – john renfrew Jun 23 '15 at 16:46

1 Answers1

2

Since you don't need to retrieve the data from an external source you can just include the variable that holds the data into the declaration of the elements you are trying to plot similar to the below for a bunch of rectangles:

d3.selectAll('rect')
  .data([VariableHere]) //no need for [] if the variable already an array
  .enter()
  .append('rect')
  .... etc.

Hope this helps!

Alex_B
  • 838
  • 7
  • 13
  • Thanks Alex The 'issue' is that the sample code does not have the js script to draw the graph inside body tags, once you do that I now have a working chart. – john renfrew Jun 23 '15 at 17:53