Search code examples
d3.jsdata-visualizationdimple.js

Is there a way to simply count instances to show on an axis in d3 or dimple?


My idea is to have a addCategoryAxis("x", '"loc"'); such as "Berlin, Deutschland" and then to count the instances of jobs that are listed with that location, so, for instance I want to add something akin to a chart.addMeasureAxis("y", '"title"';, but I don't actually care what the value of '"title"' is, I just want to count the number of times something shows up there.

At the moment I'm using Dimple, and I've been reading through the API info, it says I can do something like this, spoiler alert- this doesn't work at all*:

// Add a Brand ordinal axis on "x"
 myChart.axes.push(new dimple.axis(myChart, "x", '"loc"', null));
// Add a Sales Volume linear numerical axis on "y"
 myChart.axes.push(new dimple.axis(myChart, "y", null, '"title"'));
// // Add a time axis on "x" for the date field

*With the error Uncaught TypeError: Cannot read property '_hasCategories' of null.

Does anyone have an idea how this can be achieved with either Dimple or D3?

This is just a toy dataset which is a simulacrum of my true data file, but it's useful as a self contained testing mechanism.

<div id="chartContainer">
<script src="http://d3js.org/d3.v3.min.js"></script>
<script src="http://dimplejs.org/dist/dimple.v1.1.1.min.js"></script>
</div>

<script>

console.log(data);

var svg = dimple.newSvg("#chartContainer", 1000, 1000);
var data = [
          { '"loc"': '"Hamburg, Deutschland"', '"title"': "CNC-Dreher/in" },
          { '"loc"': 'M?nchen (Kreis), Bayern', '"title"': "Data Scientist" },
          { '"loc"': '"D?sseldorf, Nordrhein-Westfalen"', '"title"': "Praktikant m|w" },
          { '"loc"': '"Rheinland-Pfalz, Deutschland"', '"title"': "Senior Softwareentwickler Logistikdienstleistung(w/m)" },
          { '"loc"': '"D?sseldorf, Nordrhein-Westfalen"', '"title"': "Projektmanager (m/w) Information Engineering" },
          { '"loc"': '"Grimma, Leipzig (Kreis)"', '"title"': "Softwareentwickler (m/w)" },
          { '"loc"': '"Hannover, Region Hannover (Kreis)"', '"title"': "Bauleiter (m/w)" },
          { '"loc"': '"M?nchen (Kreis), Bayern"', '"title"': "Backend Software Developer (m/f)" },
          { '"loc"': '"Ratingen, Mettmann (Kreis)"', '"title"': "Ausbildung Industriekaufmann (m/w)" },
          { '"loc"': '"Bayern, Deutschland"', '"title"': "Dualer Studiengang Bachelor of Arts (Fachrichtung Bank/Vertrieb) Bayern" },
          { '"loc"': '"Landshut, Landshut (Kreis)"', '"title"': "Spezialist CFK m/w" },
          { '"loc"': '"Frankfurt am Main, Hessen"', '"title"': "Data Analyst / Consultant analytisches CRM (m/w) f?r unseren Premium Partner EDEKA" },
          { '"loc"': '"Hamburg, Deutschland"', '"title"': "Produktmanager (m/w) im Bereich Privat Kraft" },
          { '"loc"': '"Unterf?hring, M?nchen (Kreis)"', '"title"': "Controller / Assistent der Gesch?ftsleitung" },
          { '"loc"': '"Berlin, Deutschland"', '"title"': "Projektmanager (m/w)" },
          { '"loc"': 'Royal Leamington Spa', '"title"': "SOFTWARE TEST ENGINEER / QA (m/w)" },
          { '"loc"': '"Lauf an der Pegnitz, N?rnberger Land (Kreis)"', '"title"': "Bilanzbuchhalter/in" },
          { '"loc"': '"Buchenb?hl, N?rnberg"', '"title"': "Inhouse Business Intelligence Berater (m/w)" },
          { '"loc"': '"Berlin, Deutschland"', '"title"': "Praktikant Vertriebscontrolling (m/w) f?r die Dauer von 6 Monaten (Pflichtpraktikum)" }
        ];
var chart = new dimple.chart(svg,data);
chart.setBounds(100, 100, 500, 300);

// Add a Brand ordinal axis on "x"
var x =  chart.axes.push(new dimple.axis(chart, "x", '"loc"', null));
// Add a Sales Volume linear numerical axis on "y"
var y =  chart.axes.push(new dimple.axis(chart, "y", null, '"title"'));
// // Add a time axis on "x" for the date field

var lines = chart.addSeries(["project"], dimple.bar, [x, y]);

lines.data = data;
lines.lineWeight = 5;
lines.lineMarkers = true;

chart.draw();

</script>

Solution

  • So in the question I had :

    var x =  chart.axes.push(new dimple.axis(chart, "x", '"loc"', null));
    
    var y =  chart.axes.push(new dimple.axis(chart, "y", null, '"title"'));
    

    I changed this to :

    var x = chart.addCategoryAxis("x", '"loc"');
    
    var y = chart.addMeasureAxis("y", '"title"');
    

    And it worked. Updated code :

    <div id="chartContainer">
    <script src="http://d3js.org/d3.v3.min.js"></script>
    <script src="http://dimplejs.org/dist/dimple.v1.1.1.min.js"></script>
    </div>
    
    <script>
    
    console.log(data);
    
    var svg = dimple.newSvg("#chartContainer", 1000, 1000);
    var data = [
              { '"loc"': '"Hamburg, Deutschland"', '"title"': "CNC-Dreher/in" },
              { '"loc"': 'M?nchen (Kreis), Bayern', '"title"': "Data Scientist" },
              { '"loc"': '"D?sseldorf, Nordrhein-Westfalen"', '"title"': "Praktikant m|w" },
              { '"loc"': '"Rheinland-Pfalz, Deutschland"', '"title"': "Senior Softwareentwickler Logistikdienstleistung(w/m)" },
              { '"loc"': '"D?sseldorf, Nordrhein-Westfalen"', '"title"': "Projektmanager (m/w) Information Engineering" },
              { '"loc"': '"Grimma, Leipzig (Kreis)"', '"title"': "Softwareentwickler (m/w)" },
              { '"loc"': '"Hannover, Region Hannover (Kreis)"', '"title"': "Bauleiter (m/w)" },
              { '"loc"': '"M?nchen (Kreis), Bayern"', '"title"': "Backend Software Developer (m/f)" },
              { '"loc"': '"Ratingen, Mettmann (Kreis)"', '"title"': "Ausbildung Industriekaufmann (m/w)" },
              { '"loc"': '"Bayern, Deutschland"', '"title"': "Dualer Studiengang Bachelor of Arts (Fachrichtung Bank/Vertrieb) Bayern" },
              { '"loc"': '"Landshut, Landshut (Kreis)"', '"title"': "Spezialist CFK m/w" },
              { '"loc"': '"Frankfurt am Main, Hessen"', '"title"': "Data Analyst / Consultant analytisches CRM (m/w) f?r unseren Premium Partner EDEKA" },
              { '"loc"': '"Hamburg, Deutschland"', '"title"': "Produktmanager (m/w) im Bereich Privat Kraft" },
              { '"loc"': '"Unterf?hring, M?nchen (Kreis)"', '"title"': "Controller / Assistent der Gesch?ftsleitung" },
              { '"loc"': '"Berlin, Deutschland"', '"title"': "Projektmanager (m/w)" },
              { '"loc"': 'Royal Leamington Spa', '"title"': "SOFTWARE TEST ENGINEER / QA (m/w)" },
              { '"loc"': '"Lauf an der Pegnitz, N?rnberger Land (Kreis)"', '"title"': "Bilanzbuchhalter/in" },
              { '"loc"': '"Buchenb?hl, N?rnberg"', '"title"': "Inhouse Business Intelligence Berater (m/w)" },
              { '"loc"': '"Berlin, Deutschland"', '"title"': "Praktikant Vertriebscontrolling (m/w) f?r die Dauer von 6 Monaten (Pflichtpraktikum)" }
            ];
    var chart = new dimple.chart(svg,data);
    chart.setBounds(100, 100, 500, 300);
    
    // // Add a Brand ordinal axis on "x"
    // var x =  chart.axes.push(new dimple.axis(chart, "x", '"loc"', null));
    // // Add a Sales Volume linear numerical axis on "y"
    // var y =  chart.axes.push(new dimple.axis(chart, "y", null, '"title"'));
    // // // Add a time axis on "x" for the date field
    
    // chart.addCategoryAxis("x", '"loc"');
    // chart.addMeasureAxis("y", '"title"');
    
    var x = chart.addCategoryAxis("x", '"loc"');
    var y = chart.addMeasureAxis("y", '"title"');
    
    var lines = chart.addSeries(["project"], dimple.bar, [x, y]);
    
    lines.data = data;
    lines.lineWeight = 5;
    lines.lineMarkers = true;
    
    chart.draw();
    
    </script>