Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Shrink the size of bars in simple bar charts in amcharts

http://www.amcharts.com/demos/simple-column-chart/#theme-none I'm following this script

var chart = AmCharts.makeChart("chartdiv", {
    "type": "serial",
    "theme": "none",
    "dataProvider": [{
        "country": "USA",
        "visits": 2025
    }, {
        "country": "China",
        "visits": 1882
    }, {
        "country": "Japan",
        "visits": 1809
    }, {
        "country": "Germany",
        "visits": 1322
    }, {
        "country": "UK",
        "visits": 1122
    }, {
        "country": "France",
        "visits": 1114
    }, {
        "country": "India",
        "visits": 984
    }, {
        "country": "Spain",
        "visits": 711
    }, {
        "country": "Netherlands",
        "visits": 665
    }, {
        "country": "Russia",
        "visits": 580
    }, {
        "country": "South Korea",
        "visits": 443
    }, {
        "country": "Canada",
        "visits": 441
    }, {
        "country": "Brazil",
        "visits": 395
    }],
    "valueAxes": [{
        "gridColor":"#FFFFFF",
        "gridAlpha": 0.2,
        "dashLength": 0
    }],
    "gridAboveGraphs": true,
    "startDuration": 1,
    "graphs": [{
        "balloonText": "[[category]]: <b>[[value]]</b>",
        "fillAlphas": 0.8,
        "lineAlpha": 0.2,
        "type": "column",
        "valueField": "visits"      
    }],
    "chartCursor": {
        "categoryBalloonEnabled": false,
        "cursorAlpha": 0,
        "zoomable": false
    },
    "categoryField": "country",
    "categoryAxis": {
        "gridPosition": "start",
        "gridAlpha": 0,
         "tickPosition":"start",
         "tickLength":20
    },
    "exportConfig":{
      "menuTop": 0,
      "menuItems": [{
      "icon": '/lib/3/images/export.png',
      "format": 'png'     
      }]  
    }
});

When I remove all data except 2 countries (I mean 2 bars) then the width of bar is increased to the full page size.

<html>
  <head>
    <script type="text/javascript" src="http://www.amcharts.com/lib/3/amcharts.js"></script>
<script type="text/javascript" src="http://www.amcharts.com/lib/3/serial.js"></script>
<script type="text/javascript" src="http://www.amcharts.com/lib/3/themes/none.js"></script>
    <script>
    var chart = AmCharts.makeChart("chartdiv", {
    "type": "serial",
    "theme": "none",
    "dataProvider": [{
        "country": "USA",
        "visits": 2025
    }, {
        "country": "China",
        "visits": 1882
    }],
    "valueAxes": [{
        "gridColor":"#FFFFFF",
        "gridAlpha": 0.2,
        "dashLength": 0
    }],
    "gridAboveGraphs": true,
    "startDuration": 1,
    "graphs": [{
        "balloonText": "[[category]]: <b>[[value]]</b>",
        "fillAlphas": 0.8,
        "lineAlpha": 0.2,
        "type": "column",
        "valueField": "visits"      
    }],
    "chartCursor": {
        "categoryBalloonEnabled": false,
        "cursorAlpha": 0,
        "zoomable": false
    },
    "categoryField": "country",
    "categoryAxis": {
        "gridPosition": "start",
        "gridAlpha": 0,
         "tickPosition":"start",
         "tickLength":20
    },
    "exportConfig":{
      "menuTop": 0,
      "menuItems": [{
      "icon": '/lib/3/images/export.png',
      "format": 'png'     
      }]  
    }
});
</script>
<style type="text/css">
#chartdiv {
    width       : 100%;
    height      : 500px;
    font-size   : 11px;
}   
</style>
  </head>
  <body>
    <div id="chartdiv"></div>
  </body>
</html>

I tried even removing width 100% in css but no success.

I just want if there is one data then print one bar chart of a small(fixed) size. if there are more than 1 then should append autoomaticaally ... (not concerned about space left in right side)

like image 887
xyz Avatar asked Mar 17 '23 18:03

xyz


1 Answers

You can easily force all columns at the same constant pixel width using graph.fixedColumnWidth property.

Here's the related documentation:

http://docs.amcharts.com/3/javascriptcharts/AmGraph#fixedColumnWidth

Code:

"graphs": [{
    "balloonText": "[[category]]: <b>[[value]]</b>",
    "fillAlphas": 0.8,
    "lineAlpha": 0.2,
    "type": "column",
    "valueField": "visits",
    "fixedColumnWidth": 50
}],

And working example: http://jsfiddle.net/amcharts/nctpzzbr/


If you need the chart container to grow/shrink depending on actual number of elements in the chart, it's more complicated but also possible.

For that we will use the "AmCharts.addInitHandler" property to set our own function to execute before the chart is rendered:

// this will get executed BEFORE chart is drawn
// we'll use it to modify the width of the chart area
AmCharts.addInitHandler(function(chart) {
    // total offsets reserved for margins
    var offsets = 100;

    // pixel value reserved for each category
    var categoryWidth = 80;

    // calculate width
    var width = offsets + chart.dataProvider.length * categoryWidth;

    // set container width
    document.getElementById("chartdiv").style.width = "" + width + "px";

    // since the chart is already build for the initial container size we need to
    // revalidate it's size. we'll delay a little bit the call to invalidateSize()
    // so that the chart elements are created first
    setTimeout(function () {
        chart.invalidateSize();
        document.getElementById("chartdiv").style.display = "block";
    }, 1);
}, ['serial']);

And here's a working example with the above code in use: http://jsfiddle.net/amcharts/tc3uf92e/

like image 189
martynasma Avatar answered Apr 01 '23 17:04

martynasma