Interaction with Scatter Plot

40 views
Skip to first unread message

Maxim Colls

unread,
Sep 23, 2011, 5:09:57 AM9/23/11
to Google Visualization API
Hi,

I'm trying to interact with the Scatter Plot. What I want to do is to
highlight / change the color of a point dynamically.

I tried with setSelection method but it doesn't seem to work.

Thanks,

Màxim

Riccardo Govoni ☢

unread,
Sep 23, 2011, 10:48:40 AM9/23/11
to google-visua...@googlegroups.com
At the moment the scatter plot does not offer any visual highlight for selection events (that is, the selection is registered, but the selected point is not visually differentiated from the others, see https://groups.google.com/d/topic/google-visualization-api/jwLMkhwKP6c/discussion ), so plain setSelection won't work for you. Other team members might detail whether changes are happening in this area or not, because I'm not fully up-to-date on this.

In the meanwhile, since the scatterplot uses a separate color for each data serie (each datatable column) an alternative solution might be the following:

- when a point is selected (via setSelection or by user click), identify the correspondent datatable row and move the cell containing the point value to a separate column (this will trigger a color change) and redraw the visualization.
- when another point is selected, revert the previous change and apply the previous step.

I haven't explicitly coded this, but I think it should work, although it's a non trivial amount of helper code to write.

-- R.


--
You received this message because you are subscribed to the Google Groups "Google Visualization API" group.
To post to this group, send email to google-visua...@googlegroups.com.
To unsubscribe from this group, send email to google-visualizati...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/google-visualization-api?hl=en.


asgallant

unread,
Sep 23, 2011, 11:30:18 AM9/23/11
to google-visua...@googlegroups.com
It shouldn't be that hard to implement:

/*  this adds a 'select' event listner that removes a point
 *  from column 1 and adds it to column 2 (which changes 
 *  the point's color)
 *  assumes:
 *    data is the DataTable object, with data in column 1 and an empty column 2
 *    chart is the Chart object
 *    options is an object of chart options
 *  "highlighted" elements that are re-selected stay highlighted
 *  "highlighted" elements that are not re-selected get returned
 *  to column 1
 */
new google.events.addListener(chart'select'function({
    // only change if data in column 1 is selected
    if (data.getSelection()['column'== 1{
        // return previous highlighted elements to column 1
        for (0data.getNumberOfRowsi++{
            if (data.getValue(12!= null{
                data.setValue(i1data.getValue(i2));
                data.setValue(i2null);
            }
        }
        // add this element to column 2
        var row data.getSelection()['row'];
        data.setValue(rowcolnull);
        data.setValue(row2data.getValue(row1));
        chart.draw(dataoptions);
    }
});

Riccardo Govoni ☢

unread,
Sep 23, 2011, 11:55:37 AM9/23/11
to google-visua...@googlegroups.com
You're right, for some reason I ended up thinking you'd need to generate/destroy rows, while you can just use nulls in place of values during the shift. Thanks.

You might want to cache the last shifted value, to avoid having to scan the entire datatable on each 'select' event to unshift it.

Also, I think you want to set the col2 value before erasing col1 value to null.

Working demo: http://jsfiddle.net/Ry6AA/3/  ( which deselects if you click on a previously selected item).

-- R.

--
You received this message because you are subscribed to the Google Groups "Google Visualization API" group.

asgallant

unread,
Sep 23, 2011, 12:32:57 PM9/23/11
to google-visua...@googlegroups.com
D'oh!  *sheepish grin* Yeah, writing and then erasing would be a good idea.  I initially wrote it with the value stored in a variable prior to erasing the cell, but then changed it when I realized I only needed it once.  Didn't think that through too far  >;o)

asgallant

unread,
Sep 23, 2011, 12:43:47 PM9/23/11
to google-visua...@googlegroups.com
You need a minor tweak in your code as well, as row 0 never gets deselected: http://jsfiddle.net/Ry6AA/4/

Maxim Colls

unread,
Sep 24, 2011, 6:26:26 AM9/24/11
to Google Visualization API
Thank you very much! I did what you said and it perfectly works. Just
one thing, there is a little delay because you actually redraw the
complete plot and there are about 300 dots, but it works fine :)
Reply all
Reply to author
Forward
0 new messages