Sanjay Bathre
Sanjay Bathre

Reputation: 450

how to use Bootstrap font icons on x-axis lables in highcharts

i am trying to put bootstrap font icons in my highchart x-axis labels, i have put that in a fiddle, to that you can see it,

http://jsfiddle.net/sanjaybathre/cJRMx/

i am using .net api of highchart code is:

public static string SocialBarStats()
    {
        Highcharts chart = new Highcharts("chart_SocialBarStats")
            .InitChart(new Chart { DefaultSeriesType = ChartTypes.Bar })
            .SetTitle(new Title { Text = "PAGEVIEWS PER VISIT" })
            .SetXAxis(new XAxis
            {
                Categories = new[] { "<i class=\"icon-facebook\"></i>", "<i class=\"icon-facebook\"></i>",
                    "<i class=\"icon-facebook\"></i>", "<i class=\"icon-facebook\"></i>", "<i class=\"icon-facebook\"></i>" },
                Title = new XAxisTitle { Text = string.Empty }
            })
            .SetYAxis(new YAxis
            {
                Min = 0,
                Title = new YAxisTitle
                {
                    Text = "",
                    Align = AxisTitleAligns.High
                }
            })
            .SetTooltip(new Tooltip { Formatter = "function() { return ''+ this.series.name +': '+ this.y +' millions'; }" })
            .SetPlotOptions(new PlotOptions
            {
                Bar = new PlotOptionsBar
                {
                    DataLabels = new PlotOptionsBarDataLabels { Enabled = true }
                }
            })
            .SetLegend(new Legend
            {
                Enabled = false
            })
            .SetCredits(new Credits { Enabled = false })
            .SetSeries(new[]
                       {
                           new Series { Name = "Facebook", Data = new Data(new object[] { 2.5 }), Color = System.Drawing.Color.FromName("'#4c66a4'")},
                           new Series { Name = "Pinterest", Data = new Data(new object[] { 1.2 }), Color = System.Drawing.Color.FromName("'#cc181e'") },
                           new Series { Name = "Youtube", Data = new Data(new object[] { 1.8 }), Color = System.Drawing.Color.FromName("'#a825a9'") },
                           new Series { Name = "Twitter", Data = new Data(new object[] { 2.3 }), Color = System.Drawing.Color.FromName("'#129fca'") },
                           new Series { Name = "Google Plus", Data = new Data(new object[] { 2.9 }), Color = System.Drawing.Color.FromName("'#00933B'") }
                       });

        return chart.ToHtmlString();
    }

Upvotes: 2

Views: 2317

Answers (2)

Irvin Dominin
Irvin Dominin

Reputation: 30993

You can use a custom HTML formatter with useHTML and apply the according image based on the element value.

Code:

xAxis: {
    categories: ['Google Plus', 'Twitter', 'Youtube', 'Pinterest', 'Facebook'],
    title: {
        text: ''
    },
    labels: {
        useHTML: true,
        formatter: function () {
            return {
                'Google Plus': '<i class="icon-google-plus"></i>',
                    'Twitter': '<i class="icon-twitter"></i>',
                    'Facebook': '<i class="icon-facebook"></i>',
                    'Pinterest': '<i class="icon-pinterest"></i>',
                    'Youtube': '<i class="icon-youtube"></i>'
            }[this.value]; 
        }
    }
},

Demo: http://jsfiddle.net/IrvinDominin/XB7Nw/

Upvotes: 2

Sebastian Bochan
Sebastian Bochan

Reputation: 37588

You need to set useHTML as true, and add image in the labels formatter.

 xAxis: {
        labels: {
            useHTML:true,
            formatter:function() {
                return '<img src="http://www.highcharts.com/demo/gfx/snow.png" />' + this.value;
            }
        }
    },

Example: http://jsfiddle.net/7yJf6/

Upvotes: 1

Related Questions