Asynchronous Drill Down Options in FlexChart WPF Chart Controls
We often use charts for visualizing and analyzing our data. However, charts are only capable of visualizing flat (non-relational) data. But what if the data is relational or contains some hierarchy?
This is where Drill Down in charts steps in. A drill down is when you click on an individual data plot in the original or parent chart to open a new descendant chart containing additional data relevant to the plot. This additional/child data could be fetched from an API/Database on demand per your requirements. With a large enough data set, you can create endless levels of drill down.
Interested in other FlexChart controls? Download ComponentOne Today!
This blog will show how to implement asynchronous drill down with ComponentOne WPF FlexChart.
For our use case, we will be using cryptocurrency exchanges where we will be drilling down to exchange’s listed currencies and their price charts.
Creating Data Source
Our first step is to create some structures to represent exchange, currencies & price data. So, we will use the following classes for the same.
And for storing/fetching exchange, currencies & price data, we will use the following DataService class. In short, this class will mimic a REST API, which will fetch respective data during drill down.
Creating Asynchronous Drill Down Manager
Now, we have our data source ready. We will implement a drill-down manager, allowing us to perform drill-down with FlexChart.
As you can see in the above code, there is a method named AddDrillDownStep.
This method will allow us to register multiple drill-down steps (implementing IDrillDownStep) with the drill-down manager, which will be executed in the order they are registered. We will discuss the drill down steps later in this blog.
Moving to the most important part, we’ll be using IDrillDownManager’s DrillDown method to asynchronously drill down in FlexChart.
In the above code, you can see the CanDrillDown method, which basically checks if the drill down is allowed by using IDrillDownStep’s CanExecute method. And if the drill-down is allowed, the drill-down step gets executed and added to the stack containing the history (drilled steps) for drill-up operations.
Note: IDrillDownStep’s CanExecute method will contain the logic to allow/prevent drill down on FlexChart based on some conditions. For example, Drill down only if the mouse click is within the 5 pixels distance from the data point.
Similarly, we could implement the drill-up operation by executing the steps stored in the history stack. However, to keep this blog short, we won’t be including the code snippet for the DrillUp method. Please refer to the sample for full implementation.
Creating Drill Down Steps
By now, you understand that our drill-down steps will implement the IDrillDownStep interface. The implementation of this interface will contain the logic to configure FlexChart based on the inputs received at different drill levels.
As mentioned in this blog, we will use cryptocurrency exchanges to drill down to exchange’s listed currencies and their price charts.
We will need the following steps as per our use case.
- Display a column chart of the top 5 currencies listed on the cryptocurrency exchange.
- Display a monthly candle chart of any selected currency from step 1.
- Display a weekly candle chart of any selected month candle from step 2.
- Display a daily candle chart of any selected week candle from step 3.
So, let's start by creating our first drill-down step, i.e., displaying the top 4 currencies listed on the exchange.
We have injected our DataService class into this step, which is used inside the Execute method to fetch the top 5 cryptocurrencies of an exchange. Later, we configured the FlexChart’s ItemsSource, Series, Axis, and many more properties.
You might be wondering what this StepResult is getting returned from the above code. StepResult acts as a unique identifier for each executed step.
We hope you have noticed the DrilledPath property and DrillToPath method of IDrillDownManager. DrilledPath property stores all the StepResults returned from the registered steps when executed. So, its instance can be passed to IDrillDownManager’s DrillToPath method to jump to a specific drilled-down step.
Note: Each step should return a new instance of StepResult.
Similarly, you can create the other three steps by following the abovementioned approach. (Please refer to the attached sample for the code.)
Integrating Drill Down Manager with FlexChart
We are ready with our drill down manager and steps, and now it's time to integrate it with FlexChart. Assuming you have already created a project containing FlexChart. We will now configure the initial view of FlexChart. For example, in our case, we will display a column chart showing the top 5 crypto exchanges as follows:
And the result will look as follows:
Once it is done, we will register the drill down steps with the drill down manager as follows:
Now we have set up everything that is needed for the drill down. The only thing left is to use the drill-down manager with FlexChart’s mouse click events.
We will left-click the mouse to drill down so we can use IDrillDownManager’s DrillDown method inside FlexChart’s MouseLeftButtonDown event as follows:
Similarly, for drilling up, we will right-click the mouse, so we can use FlexChart’s MouseRightButtonDown event as follows:
We’re finished! We have implemented asynchronous drill-down with FlexChart.
Please feel free to comment below if you have any queries or suggestions.
Interested in other FlexChart controls? Download ComponentOne Today!