如何在点击FusionCharts 2D柱形图时更改条形不透明度?

时间:2017-04-27 19:17:58

标签: bar-chart fusioncharts

我正在尝试使用" dataPlotClick"点击它时切换条形图的不透明度。事件。我能够改变不透明度,但是只要我将鼠标移出条形图,不透明度就会变回默认值。

注意:之前有人问过类似的问题,但似乎没有一个有效的答案。

如何利用以下dataPlotClick事件来实现这一目标?

                            events: {
                            "dataPlotClick": function (evtObj, argObj) {
                                var i, plotItems;
                                plotItems = evtObj.sender.jsVars.hcObj.elements.plots[0].items;                                   
                                plotItems[argObj.dataIndex].graphic.attr("fill-opacity", .2);
                            }
                        }

Fusion Chart:

FusionCharts.ready(function () {
var revenueChart = new FusionCharts({
    type: 'column2d',
    renderAt: 'chart-container',
    width: '500',
    height: '350',
    dataFormat: 'json',
    dataSource: {
        "chart": {
            "caption": "Monthly revenue for last year",
                "subCaption": "Harry's SuperMart",
                "xAxisName": "Month",
                "yAxisName": "Revenue (In USD)",
                "numberPrefix": "$",
                "paletteColors": "#0075c2",
                "bgColor": "#ffffff",
                "borderAlpha": "20",
                "canvasBorderAlpha": "0",
                "usePlotGradientColor": "0",
                "plotBorderAlpha": "10",
                "placevaluesInside": "1",
                "rotatevalues": "1",
                "valueFontColor": "#ffffff",
                "showXAxisLine": "1",
                "xAxisLineColor": "#999999",
                "divlineColor": "#999999",
                "divLineIsDashed": "1",
                "showAlternateHGridColor": "0",
                "subcaptionFontBold": "0",
                "subcaptionFontSize": "14"
        },
       "data": [
       {
            "label": "Jan",
                "value": "420000"
        }, {
            "label": "Feb",
                "value": "810000"
        }, {
            "label": "Mar",
                "value": "720000"
        }, {
            "label": "Apr",
                "value": "550000"
        }, {
            "label": "May",
                "value": "910000"
        }, {
            "label": "Jun",
                "value": "510000"
        }, {
            "label": "Jul",
                "value": "680000"
        }, {
            "label": "Aug",
                "value": "620000"
        }, {
            "label": "Sep",
                "value": "610000"
        }, {
            "label": "Oct",
                "value": "490000"
        }, {
            "label": "Nov",
                "value": "900000"
        }, {
            "label": "Dec",
                "value": "730000"
        }]
    },
                        events: {
                            "dataPlotClick": function (evtObj, argObj) {
                                var i, plotItems, plotLength;
                                plotItems = evtObj.sender.jsVars.hcObj.elements.plots[0].items;
                                plotItems[argObj.dataIndex].graphic.attr("fill-opacity", .2);
                            }
                        }
    }
}).render();
});

1 个答案:

答案 0 :(得分:3)

alpha 属性需要在dataPlotClick FusionCharts API事件中为已点击的特定数据图设置。

可以在dataPlotClick FusionCharts API事件中的dataObj参数dataIndex属性的帮助下找到特定的数据绘图索引。

最后在FusionCharts setJSONData API方法的帮助下设置修改后的JSON结构。

请执行以下给定的代码。

FusionCharts.ready(function () {
    var topStores = new FusionCharts({
        type: 'column2d',
        renderAt: 'chart-container',
        width: '400',
        height: '300',
        dataFormat: 'json',
        dataSource:  {
        "chart": {
          "caption": "Monthly revenue for last year",
          "subCaption": "Harry's SuperMart",
          "xAxisName": "Month",
          "yAxisName": "Revenue (In USD)",
          "numberPrefix": "$",
          "paletteColors": "#0075c2",
          "bgColor": "#ffffff",
          "borderAlpha": "20",
          "canvasBorderAlpha": "0",
          "usePlotGradientColor": "0",
          "plotBorderAlpha": "10",
          "placevaluesInside": "1",
          "rotatevalues": "1",
          "valueFontColor": "#ffffff",
          "showXAxisLine": "1",
          "xAxisLineColor": "#999999",
          "divlineColor": "#999999",
          "divLineIsDashed": "1",
          "showAlternateHGridColor": "0",
          "subcaptionFontBold": "0",
          "subcaptionFontSize": "14"
        },
        "data": [{
          "label": "Jan",
          "value": "420000"
        }, {
          "label": "Feb",
          "value": "810000"
        }, {
          "label": "Mar",
          "value": "720000"
        }, {
          "label": "Apr",
          "value": "550000"
        }, {
          "label": "May",
          "value": "910000"
        }, {
          "label": "Jun",
          "value": "510000"
        }, {
          "label": "Jul",
          "value": "680000"
        }, {
          "label": "Aug",
          "value": "620000"
        }, {
          "label": "Sep",
          "value": "610000"
        }, {
          "label": "Oct",
          "value": "490000"
        }, {
          "label": "Nov",
          "value": "900000"
        }, {
          "label": "Dec",
          "value": "730000"
        }]
      },
      events: {
        "dataPlotClick": function(evtObj, argObj) {

          var plotNum= argObj.dataIndex;
          var data =evtObj.sender.args.dataSource.data;
          data[plotNum].alpha="10";
          evtObj.sender.setJSONData(evtObj.sender.args.dataSource);

        }
      }
    })
    .render();
});