admin管理员组

文章数量:1317909

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=".js"></script>
<script type="text/javascript" src=".js"></script>
<script type="text/javascript" src=".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)

http://www.amcharts./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./lib/3/amcharts.js"></script>
<script type="text/javascript" src="http://www.amcharts./lib/3/serial.js"></script>
<script type="text/javascript" src="http://www.amcharts./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)

Share Improve this question edited Dec 3, 2014 at 19:03 Erik BRB 3156 silver badges20 bronze badges asked Dec 3, 2014 at 15:29 xyzxyz 1971 gold badge3 silver badges17 bronze badges 1
  • There's HTML and CSS you're not showing, please paste in the relevant code in your question (links can go bad, so all the info should be inside your question). – Josh KG Commented Dec 3, 2014 at 15:58
Add a ment  | 

1 Answer 1

Reset to default 7

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

Here's the related documentation:

http://docs.amcharts./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/amcharts/nctpzzbr/


If you need the chart container to grow/shrink depending on actual number of elements in the chart, it's more plicated 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/amcharts/tc3uf92e/

本文标签: javascriptShrink the size of bars in simple bar charts in amchartsStack Overflow