Accepted answer

you can use jquery.extend() and highcharts.setoptions.
so first you'll make the first object which will be extended by all your charts, this object will contain your highchart default functions.

you can do it using namespacing.
the following way is good when you have very different charts.

default graphic:

var defaultchart = {
    chartcontent: null,
    highchart: null,
    defaults: {

        chart: {
            alignticks: false,
            bordercolor: '#656565',
            borderwidth: 1,
            zoomtype: 'x',
            height: 400,
            width: 800

        series: []


    // here you'll merge the defauls with the object options

    init: function(options) {

        this.highchart= jquery.extend({}, this.defaults, options);
        this.highchart.chart.renderto = this.chartcontent;

    create: function() {

        new highcharts.chart(this.highchart);


now, if you want to make a column chart, you'll extend defaultchart

var columnchart = {

    chartcontent: '#yourchartcontent',
    options: {

        // your chart options


columnchart = jquery.extend(true, {}, defaultchart, columnchart);

// now columnchart has all defaultchart functions

// now you'll init the object with your chart options


// when you want to create the chart you just call


if you have similar charts use highcharts.setoptions which will apply the options for all created charts after this.

// `options` will be used by all charts

// only data options
var chart1 = highcharts.chart({
    chart: {
        renderto: 'container1'
    series: []

var chart2 = highcharts.chart({
    chart: {
        renderto: 'container2'
    series: []


complete demo


to add to @ricardo's great answer, i have also done something very similar. in fact, i won't be wrong if i said i went a step further than this. hence would like to share the approach.

i have created a wrapper over the highchart library. this gives multiple benefits, following being the main advantages that encouraged going in this path

  • decoupling: decouples your code from highcharts
  • easy upgrades: this wrapper will be the only code that will require modification in case of any breaking changes in highchart api after upgrades, or even if one decides to move to a differnt charting library altogether (even from highchart to highstock can be exhaustive if your application uses charts extensively)
  • easy of use: the wrapper api is kept very simple, only things that may vary are exposed as options (that too whose values won't be as a deep js object like hc already has, mostly 1 level deep), each having a default value. so most of the time our chart creation is very short, with the constructor taking 1 options object with merely 4-5 properties whose defaults don't suit the chart under creation
  • consistent ux: consistent look & feel across the application. eg: tool tip format & position, colors, font family, colors, toolbar (exporting) buttons, etc
  • avoid duplication: of course as a valid answer of the asked question it has to avoid duplication, and it does to a huge extent

here is what the options look like with their default values

defaults : {
        charttype : "line", 
        starttime : 0,
        interval : 1000,
        chartdata : [],
        title : "product name",
        navigator : true,
        legends : true,
        presettimeranges : [],
        primarytoolbarbuttons : true,
        secondarytoolbarbuttons : true,
        zoomx : true,
        zoomy : false,
        height : null,
        width : null,
        panning : false,
        reflow : false,
        ydecimals : 2,
        container : "container",
        allowfullscreen : true,
        credits : false,
        showall : false,
        fontsize : "normal", // other option available is "small"
        showbtnsinnewtab : false,
        xaxistitle : null,
        yaxistitle : null,
        onload : null,
        pointmarkers : false,
        categories : []

as you can see, most of the times, its just chartdata that changes. even if you need to set some property, its mainly just true/false types, nothing like the horror that highchart constructor expects (not critizing them, the amount of options they provide is just amazing from customization point of view, but for every developer in the team to understand & master it can take some time)

so creation of chart is as simple as

var chart=new mylib.chart({
              chartdata : [[1000000,1],[2000000,2],[3000000,1],[4000000,5]]


i know this has already been answered, but i feel that it can be taken yet further. i'm still newish to javascript and jquery, so if anyone finds anything wrong, or thinks that this approach breaks guidelines or rules-of-thumb of some kind, i'd be grateful for feedback.

building on the principles described by ricardo lohmann, i've created a jquery plugin, which (in my opinion) allows highcharts to work more seamlessly with jquery (i.e. the way that jquery works with other html objects).

i've never liked the fact that you have to supply an object id to highcharts before it draws the chart. so with the plug-in, i can assign the chart to the standard jquery selector object, without having to give the containing <div> an id value.

    var charttype = {
        myarea : {
            chart: { type: 'area' },
            title: { text: 'example line chart' },
            xaxis: { /* xaxis settings... */ },
            yaxis: { /* yaxis settings... */ },
            /* etc. */
            series: []
        mycolumn : {
            chart: { type: 'column' },
            title: { text: 'example column chart' },
            xaxis: { /* xaxis settings... */ },
            yaxis: { /* yaxis settings... */ },
            /* etc. */
            series: []
    var methods = {
            function (chartname, options) {
                return this.each(function(i) {
                    optsthis = options[i];
                    charttype[chartname].chart.renderto = this;
                    optshighchart = $.extend (true, {}, charttype[chartname], optsthis);
                    new highcharts.chart (optshighchart);
    $.fn.cbhchart = function (action,objsettings) {
        if ( charttype[action] ) {
            return methods.init.apply( this, arguments );
        } else if ( methods[action] ) {
            return methods[method].apply(this,,1));
        } else if ( typeof action === 'object' || !action ) {
            $.error( 'invalid arguments to plugin: jquery.cbhchart' );
        } else {
           $.error( 'action "' +  action + '" does not exist on jquery.cbhchart' );

with this plug-in, i can now assign a chart as follows:

$('.columnchart').cbhchart('mycolumn', optionsarray);

this is a simplistic example of course; for a real example, you'd have to create more complex chart-properties. but it's the principles that concern us here, and i find that this approach addresses the original question. it re-uses code, while still allowing for individual chart alterations to be applied progressively on top of each other.

in principle, it also allows you to group together multiple ajax calls into one, pushing each graph's options and data into a single javascript array.

the obligatory jfiddle example is here:

criticism welcome!!

Related Query

More Query from same tag