|
Highlighting a single bar in a Bar chart using c# |
Posted by James on Mar-15-2011 00:19 |
|
Hi
I got a standard bar graph with some classes derived from WinChartViewer. Tool tips are set up but now theres a requirment that individual bars be highlighted on a mouse over. The mouse over capture is the same as the tool tip. Need to know in general what methods i can use to highlight an individual Bar?
Thanks |
Re: Highlighting a single bar in a Bar chart using c# |
Posted by Peter Kwan on Mar-15-2011 11:58 |
|
Hi James,
ChartDirector bars can be in 3D and of arbitrary polygonal shapes (not necessarily rectangular). The methods to highlight a bar depends on exactly what you want to do to highlight the bar.
The followings are some options:
(A) Many common GUI now highlight something (including bars) by simply putting a "dot" on the data value position (typically the top point of the bar).
To put a dot on a chart, you may use another control (say a text box control), set the size of the control to the size of the dot (say 11 x 11 px), and set the background color to the color of the dot (say black). Then when the mouse is over a bar, you may put the dot control on the bar. When the mouse leaves a bar, you may hide the control.
To determine the pixel coordinate to position the dot, you may use XYChart.getXCoor and XYChart.getYCoor to get the pixel coordinates from the data values. The data x-coordinate of a bar (assuming you are using label based x-axis) is simply 0, 1, 2, .... (for the first, second, third, .... bars). The y-coordinate at the top of the bar is the bar data value.
(B) If the bars are rectangular and you want to change the color of the bar, or to draw a border to highlight the bar, you may use similar method as (A) above. To change the color of the bar, simply resize the dot so that it is as large as the bar. To draw a border, simply resize the dot so that it is as large as the bar, and set the dot to use a transparent fill color and the border color of your choice.
To achieve the above, you would need to know the bar width. The bar width is given by:
barWidth = plotAreaWidth / noOfBars * (1 - barGap)
The barGap is configurable using BarLayer.setBarGap. The default is 0.75.
(C) Some people just like the vertical strip where the bar belongs, instead of the bar. In this case, you can use the same method as (B), but set the rectangle to highlight the vertical strip.
(D) If you are using a 3D bar of arbitrary shape, and you want the change the color of the entire bar, you may redraw the chart and use a different color for the bar. (You may use addBarLayer3, so that you may configure a different color for each bar.)
Hope this can help.
Regards
Peter Kwan |
Re: Highlighting a single bar in a Bar chart using c# |
Posted by James on Mar-16-2011 00:56 |
|
Hi Peter
Thanks for your help on this.
I messed around with the resizing of an edit box but never found this satisfactory. Update seemed way too slow. Using addBarLayer3 to apply a color array is a much cleaner and responsive solution.
James |
Re: Highlighting a single bar in a Bar chart using c# |
Posted by James on Mar-18-2011 21:54 |
|
Hi Peter
Im finding an issue when adding a layer using addBarLayer3. Once drawn it seems to switch off the tooltip. So mouseover -> add layer to draw highlighted bar -> but no tooltip appears. Is there a workaround for this issue?
James |
Re: Highlighting a single bar in a Bar chart using c# |
Posted by Peter Kwan on Mar-19-2011 01:59 |
|
Hi James,
The addBarLayer3 will not switch off the tooltip. There are quite a number of sample charts included in ChartDirector that uses addBarLayer3, and they all can show tooltips.
Do you mean that initially the chart shows tooltip, but after you have updated the chart, it no longer shows tooltips? When you update the chart, do you also update the image map?
When you set a new chart to WinChartViewer, it will automatically clear the image map for the old chart. If you do not set the image map, the new chart will not have any image map, and there will be no hotspots or tooltips.
If you think the image map does not need to be changed (as the new and old chart has exactly the same hot spots), you may consider to do something like:
Dim temp As String = myWinChartViewer.ImageMap 'save image map
myWinChartViewer1.Image = c.makeImage()
myWinChartViewer.ImageMap = temp 'restore the image map
Hope this can help.
Regards
Peter Kwan |
|