Your Privacy Matters: We use our own and third-party cookies to improve your experience on our website. By continuing to use the website we understand that you accept their use. Cookie Policy
330
UltraPieChart select which slices to include?
posted

I am creating my first pie chart based on data from a SQL database, and I have it showing with no issues.

But I'd like to be able to allow the user to select and de-select which slices to show. The reason for this is that sometime a particular slice is so large that it overshadows the other slices. 

I've though about different ways to do that. They could click on a slice to toggle it, but then I'm afraid that it would also disappear from the legend and so they couldn't click to get it back. 

Another option is to include a checkbox next to each of the legends items as I have mocked up in the attached picture. 

Is there an easier way to handle this?

Thanks!

  • 34810
    Verified Answer
    Offline posted

    Hello Ted,

    I have been investigating into the behavior you are looking to achieve, and while this behavior would not be simple, it can be done.

    The first thing I would recommend that you do in this case is to utilize two UltraPieCharts with one being directly on top of the other such that the one on the bottom will never be used. This is necessary unless you want to create your own custom legend element to manage, as if the data item is not plotted in the chart or if the value of its slice is zero, it will not show up in the legend. In this case, you would use a master data collection and a modifiable one so that you can modify the pie chart on top while the “bottom” one remains untouched and used with the legend.

    Next comes the actual addition of the checkbox to the legend, which I would recommend you do on Form.Load. I would recommend going into the UltraItemLegend.Controls collection and digging into it until you find a collection of FlowLayoutPanel elements with a Count equal to the number of your data items in the pie chart. You can then add your Checkbox to those data items.

    On CheckedChanged of your Checkboxes, I would recommend getting the parent FlowLayoutPanel of the Checkbox, as this will allow you to go back into its Controls collection and get the Label in the legend. You can then use the Text of this label to query your data source for the data item represented by the slice to be hidden. You can either remove the data item from your “modifiable” data source or set its value path to zero, since the master data source is actually what the legend would be using, and so the legend item would not be removed. You can get the data item for re-adding or resetting the value from the master data source.

    As an added level of difficulty to this, you will find some issues in keeping the colors of the slices of the pie chart in sync with the legend. The reason for this is that the Brushes collection of the UltraPieChart has a proportional assignation of its brush indexes to the slice indexes, meaning as the underlying pie chart will not change, but as you remove data items or set their values to zero, the slice indexes will change, but the brushes won’t. You can, however, supply your own BrushCollection to the UltraPieChart, which I would recommend that you do in a master and “modifiable” like above, and then manage these brushes accordingly to keep them in the order of elements added and removed.

    I am attaching a sample project that has a rough implementation of the brush management in this case, but it should give you an idea of the management that would need to be done to achieve this requirement.

    Please let me know if you have any other questions or concerns on this matter.

    UltraPieChartCheckboxItemDemo.zip