Bogdan Molinger
Bogdan Molinger

Reputation: 341

Coloring Highcharts category

I am trying to distinguish between multiple types of data on my column range chart. The problem is that I want to be able to group them by multiple criteria. I can easily color different columns which solves one issue, but I would like to be able to also color the category on the xAxis (the chart is inverted), acording to what data I am passing to my chart, in order to obtain something like: enter image description here Is there any way to obtain this ?

EDIT:

I'm passing my data in the form of a dictionary with multiple keys :

entries[record['url']] = {
                'location': categories.index(str(counter) + '. ' + (record['term'] if record['term'] else record['uterm'])),
                'subjectid': record['subjectid'],
                'subject': record['subject'],
                'uterm': record['uterm'],
                'term': record['uterm'] if record['term'] else 'Uncoded term',
                'start_day': start_day,
                'end_day': end_day,
                'start': record['start'] if record['start'] else oldest_date,
                'end': record['full_end'] if record['full_end'] else '',    
                'full_end': record['full_end'] if record['full_end'] != record['full_start'] and record['full_end']!= '' else datetime.now().strftime(date_format),
                'full_start': record['full_start'],
                'persistence': record['persistence'] if record['persistence'] else '',
                'serious': record['serious'] if record['serious'] else '',
                'section': record['section'] if record['section'] else '',
                'min_date': record['min_date'],
                'color':'#FF4136' if record['serious'] and record['serious'].lower() == 'yes' else '#FF7329'  

I get my data in form of a pandas dataframe from a database. I want to color code the individual bars according to the section and the category by serious using the color key

EDIT 2:

I've followed Kamil's suggestion but now I am not sure how to add data dynamically, I've tried the following:

             var data = [];
             var cols = [];
             for( elem in options_data){
                 data.push({
                     x: options_data[elem]['location'],
                     low: options_data[elem]['start_day'],
                     high: options_data[elem]['end_day'],
                     color: options_data[elem]['color']

                 });   
                 cols.push({
                     y: 0,
                     color: '#bada55'
                 });
             }
             data.join(", ");
             cols.join(", ");
             chart.addSeries([{
                 type: 'column',
                 yAxis: 1,
                 data: cols,
                 minPointLength: 10,
                 threshold: -0.1,
                 pointPadding: 0,
                 pointRange: 0,
                 groupPadding: 0,
                 showInLegend: false,
                 tooltip: {
                     pointFormat: false
                 },
                 states: {
                     hover: {
                         enabled: false
                     }
                 }                 
             },{
                 name: "Study Days",
                 data: data,
                 maxPointWidth: 30,
                 point: {
                     events: {
                         click: function() {
                             if (url != "")
                                 window.open(url,'_blank');                                 
                         }
                     }
                 }
             }]);  

This does not lead to any error, but the chart doesn't get data. This approach worked with only 1 series but not with two.

EDIT 3:

I've managed to fix the data issues. My final version of adding data is:

             chart.addSeries({
                 type: 'column',
                 yAxis: 1,
                 data: cols,                     
                 zIndex: 9999,
                 minPointLength: 10,
                 threshold: -0.1,
                 pointPadding: 0,
                 pointRange: 0,
                 groupPadding: 0,
                 showInLegend: false,
                 tooltip: {
                     pointFormat: false
                 },
                 states: {
                     hover: {
                         enabled: false
                     }
                 }                 
             });
                 chart.addSeries({
                 name: "Study Days",
                 data: data,
                 maxPointWidth: 30,
                 point: {
                     events: {
                         click: function() {
                             if (url != "")
                                 window.open(url,'_blank');                                 
                         }
                     }
                 }
             });

Upvotes: 0

Views: 706

Answers (1)

Kamil Kulig
Kamil Kulig

Reputation: 5826

You can achieve that kind of look by adding another witty configured column series and y axis associated with it:

  series: [{
    // series that mimics category markers
    type: 'column',
    yAxis: 1,
    data: [{
      y: 0,
      color: '#bada55'
    }, {
      y: 0,
      color: '#c0ffee'
    }],
    minPointLength: 10,
    threshold: -0.1,
    pointPadding: 0,
    pointRange: 0,
    groupPadding: 0,
    showInLegend: false,
    tooltip: {
      pointFormat: false
    },
    states: {
      hover: {
        enabled: false
      }
    }
  }, 
  // normal series...
],

yAxis: [{}, {
 // axis for category markers series
  tickPositions: [0, 1000],
  visible: false
}],

xAxis: {
  categories: ['apples', 'oranges'],
  offset: -10
},

Live demo: http://jsfiddle.net/BlackLabel/zfrq0tt2/

Proper Series.minPointLenght, Series.threshold and Axis.tickPositions values together with zero y values of columns cause that bars always maintain constant width. Interaction with this series can be prevented by disabling showInLegend, tooltip.pointFormat and states.hover.enabled properties.

Upvotes: 1

Related Questions