2 Replies Latest reply on Mar 8, 2007 2:29 AM by romanczar

    Force Chart redraw


      I am trying to populate a BarChart with data and I have a custom itemRenderer to render the bars in different colors depending on an array of thresholds I set. What I want to do, is later, after a button is clicked say, change the thresholds in the itemRenderer and then redraw the chart given the changes made in the itemRenderer.

      First off, I could find no way that worked to change the thresholds in the itemRenderer. So instead I put the threshold data in my data array (which sucks, because its repetitive data, but I could find no other way to do it).

      Secondly, when I click a button, i go through the data and change the threshold values. Then I want the chart to redraw. I have tried everything!!!! The only thing I have found to work is to add another item to the data, which is then added to the chart. But of course I don't want this other data item in my chart, I just want the chart to redraw with the data that's already in it! I technically am changing the data in the dataProvider, so why doesn't this cause the whole chart to re-render? I'm guessing some sort of optimization is going on here.... so is there some way to get around that?

      Thanks in advance for any advice you might be able to give.

        • 1. Re: Force Chart redraw
          peterent Level 2
          First, whenever you want to change data and have the chart or DataGrid or whatever, reflect the changes, once you've changed an item in your data use the collection's itemUpdated function:

          var item:Object = mydata.getItemAt(i);
          item.value = newvaluehere;
          mydata.itemUpdated(item); or mydata.itemUpdated(item,"value"); if you want to be specific

          You ought not to change data inside of an itemRenderer from outside the renderer. The reason is that the itemRenderers are temporary and can reassigned or even destroyed.

          Your itemRenderer should ask for new values when it needs it. It could be asking for the data from the main application or from a static variable in a class, etc. Suppose you have this class which contains the values you want:

          class MyValues {
          static public var threshold1:Number;
          static public var threshold2:Number;

          Now when you know you want to change them:
          MyValues.threshold1 = 100;
          MyValues.threshold2 = 200;

          Then in your itemRenderer's updateDisplayList function you use the values:
          if( MyValues.threshold1 > data.y1 ) ....
          if( MyValues.threshold2 > data.y2 ) ...

          I think that's the easiest way to go.
          • 2. Re: Force Chart redraw
            romanczar Level 1
            I use a custom chart renderer to get colored columns. The color come from my dataprovider as an attribute of the XML node.

            I figured out that any chart connected to this dataprovider (array collection) gets only updated if either the axis texts change or the real value of the column (or pie or bar). But if I change the property for my color this does not result in a redraw of the chart.

            How can I force a redraw without having to change either texts or values in my chart? I also tried to increase and decrease the values, but this does not have the desired effect, the chart does not get redrawn.

            Any idea?