Use NumberFormatter and BarFormatter together

39 views
Skip to first unread message

NA

unread,
May 10, 2012, 5:26:18 PM5/10/12
to google-visua...@googlegroups.com

Is it possible to use a NumberFormatter and BarFormatter at the same time on a table whose underlying DataTable will be updated? 

I am able to render a Table with both a numberformatter and barformatter applied to the same column.  That looks fine.

But when I change the values, the last formatter applied is the only one that shows the correct values; the previous formatter shows the previous values.  I've even tried removing all formatters, applying one formatter, forcing a redraw, then applying another formatter and again forcing a redraw.  But the first formatter's values are always unchanged while the second formatter has the right values.

This is especially frustrating because in the initial rendering, the column is formatted correctly by both formatters.

I feel like I've asked this before or seen a similar thread, but searching this group didn't turn anything up.

Any ideas?

Pseudo code looks like:

//  weightsTable is a DataTable
//  weightsDisplay is a ChartWrapper
//  numberFormatter and barFormatter are formatters

for {
  weightsTable.setValue(i,2,weight);
  weightsTable.setFormattedValue(i,2,null);
}

numberFormatter.format(weightsTable,2);
barFormatter.format(weightsTable,2);
weightsDisplay.draw();

The first time this code is run, everything looks great.  But when the code is called again (the table has already been rendered), I get the buggy behavior.

I might resort to just deleting the entire table, though that's not a very satisfying workaround...

thanks for any ideas,


asgallant

unread,
May 11, 2012, 9:15:13 AM5/11/12
to google-visua...@googlegroups.com
I think what you have is almost correct, it just needs a small tweak: in the loop where you reset the formatted values, try setting them to the actual value instead of null.  Some formatters modify the already-formatted value rather than the base value.  You'd want something like this (in your pseudocode):

for {
  weightsTable.setValue(i,2,weight);
  weightsTable.setFormattedValue(i,2,weight);

NA

unread,
May 14, 2012, 9:50:23 AM5/14/12
to google-visua...@googlegroups.com
I tried your idea, and it didn't work.  Same problem as without it.

I also don't understand why it works when the chart is first drawn, but not after the chart is updated.  It seems as if the only way to make progress here is to just destroy the chart and redraw it each time, which isn't desirable.

Does anyone have a working example of a number formatter and bar formatter applied to a table whose values are then updated?

asgallant

unread,
May 14, 2012, 10:57:48 AM5/14/12
to google-visua...@googlegroups.com
Ok, I did some experimenting, and it turns out that the formatter stores the old formatted value in a cell property (_bar_format_old_value) which it uses when you reformat the table.  You can get around this by using the #setCell method instead of #setValue, ie:

data.setCell(row, col, value, null, null);

The first null clears the formatted value, the second clears the cell properties.  You can then reformat as you wish.
Reply all
Reply to author
Forward
0 new messages