Search code examples
bar-chartc3

How to create stacked bar chart using C3 for string data?


I am trying to created stacked bar chart using C3 chart. But i am facing problem as i am trying to plot a graph with string values. My data looks like this

var data1=["Country", "Denmark", "India"]
var data2=["Value", 1, 1]
var X=["Item",'Item1','Item2]

c3.generate({
             data: {
                x:'Item', 
        columns:[[data1],[data2]],
                 type: 'bar' ,
        groups: [
            ['data1', 'data2']
        ]
    },);

I just want to display the values using the tooltip functionality of C3-chart.Is there a way to make my bar chart independent of the y-axis?

thanks


Solution

  • If I understand correctly, you could do something like this: http://jsbin.com/dowogejose/1/edit?html,js,output

    Basically set a value of 1 for each country:

    columns: [
      ['Denmark', 1],
      ['India', 1]
    ]
    

    Then set the callback function on the tooltip format to get the value from a lookup:

    tooltip: {
      format: {
        value: function(value, ratio, id, index) {
          var lookup = {
            'Denmark': 123,
            'India': 456
          };
          return lookup[id];
        }
      }
    }
    

    You could even store the lookup value outside of the tooltip callback, and generate the columns array using this object.