Why doesn't the C3 diagram appear?

I am trying to create a simple diagram and it just doesn't work. Any help would be great. I followed the instructions given on the C3.js documentation website, but I still get a blank page.

<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" type="text/css" href="https://rawgit.com/masayuki0812/c3/master/c3.css">
<script src="http://d3js.org/d3.v3.min.js" charset="utf-8"></script>
<script src="https://rawgit.com/masayuki0812/c3/master/c3.js"></script>

<script>
var chart = c3.generate({
bindto: '#chart',
data: {
  columns: [
    ['data1', 30, 200, 100, 400, 150, 250],
    ['data2', 50, 20, 10, 40, 15, 25]
  ]
}
});

</script>
</head>
<body>
<div id="chart"></div>
</body>
</html>

      

+3


source to share


1 answer


First, I would check for cross-origin exceptions. This usually happens through scripts hosted on other sites. If you're having problems like this, search for a Content Delivery Network (CDN). These are site host scripts that can be run on any website.

But I believe your problem is that you are running JavaScript code before the document has finished loading. There are two ways to ensure that the element is loaded before you start executing JavaScript in the DOM.

Script in HEAD (using timeout)

The source of your HTML page should look like this. You will need to wait for the item to load first. This used pure JavaScript and doesn't need jQuery.

<!DOCTYPE html>
<html>
  <head>
    <link href="https://cdnjs.cloudflare.com/ajax/libs/c3/0.4.10/c3.min.css" rel="stylesheet" />
    
    <script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.5/d3.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/c3/0.4.10/c3.min.js"></script>
    
    <script type="text/javascript">
      onReady('#chart', function() {
        var chart = c3.generate({
          data: {
            columns: [
              ['data1', 300, 350, 300, 0, 0, 0],
              ['data2', 130, 100, 140, 200, 150, 50]
            ],
            types: {
              data1: 'area',
              data2: 'area-spline'
            }
          },
          axis: {
            y: {
              padding: {
                bottom: 0
              },
              min: 0
            },
            x: {
              padding: {
                left: 0
              },
              min: 0,
              show: false
            }
          }
        });
      });
      
      // Set a timeout so that we can ensure that the `chart` element is created.
      function onReady(selector, callback) {
        var intervalID = window.setInterval(function() {
          if (document.querySelector(selector) !== undefined) {
            window.clearInterval(intervalID);
            callback.call(this);
          }
        }, 500);
      }
    </script>
  </head>
  <body>
    <div id="chart"></div>
  </body>
</html>
      

Run codeHide result


Script at the end of the DOM (no timeout)

You can also run your script after the element chart

. This script will work because the target #chart

has already been parsed by the browser and loaded.



<!DOCTYPE html>
<html>
  <head>
    <link href="https://cdnjs.cloudflare.com/ajax/libs/c3/0.4.10/c3.min.css" rel="stylesheet" />

    <script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.5/d3.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/c3/0.4.10/c3.min.js"></script>
  </head>
  <body>
    <div id="chart"></div>

    <script type="text/javascript">
      var chart = c3.generate({
        data: {
          columns: [
            ['data1', 300, 350, 300, 0, 0, 0],
            ['data2', 130, 100, 140, 200, 150, 50]
          ],
          types: {
            data1: 'area',
            data2: 'area-spline'
          }
        },
        axis: {
          y: {
            padding: {
              bottom: 0
            },
            min: 0
          },
          x: {
            padding: {
              left: 0
            },
            min: 0,
            show: false
          }
        }
      });
    </script>
  </body>
</html>
      

Run codeHide result



Fragment

Here's a working example. Make sure your paths are correct for D3 and C3 files.

var chart = c3.generate({
  data: {
    columns: [
      ['data1', 300, 350, 300, 0, 0, 0],
      ['data2', 130, 100, 140, 200, 150, 50]
    ],
    types: {
      data1: 'area',
      data2: 'area-spline'
    }
  },
  axis: {
    y: {
      padding: {
        bottom: 0
      },
      min: 0
    },
    x: {
      padding: {
        left: 0
      },
      min: 0,
      show: false
    }
  }
});
      

<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.5/d3.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/c3/0.4.10/c3.min.js"></script>
<link href="https://cdnjs.cloudflare.com/ajax/libs/c3/0.4.10/c3.min.css" rel="stylesheet" />

<div id="chart"></div>
      

Run codeHide result


+4


source







All Articles