Quick Start Guide > Tutorials (Swift3) > Tutorial 05 - Adding Tooltips and Legends
Tutorial 05 - Adding Tooltips and Legends

Adding Tooltips & Legends to a Chart

So far in the tutorial series, we have created a new chart, added an XAxis, YAxis, some series and also some zoom and pan behaviours.

Source code for this tutorial can be found at our Github Repository under v2.x > Tutorials

We're going to extend this now to add Legends and Tooltips to the chart. Modify the existing code from Tutorial 04 - Adding Zooming, Panning Behavior as follows.


Update the addModifiers() method with following code:

Adding Tooltips and Legends to the chart
Copy Code
    func addModifiers(){
       ...
        
        let rolloverModifier = SCIRolloverModifier()
        let legend = SCILegendModifier()
       
        let groupModifier = SCIChartModifierCollection(childModifiers: [xAxisDragmodifier, yAxisDragmodifier, pinchZoomModifier, extendZoomModifier, legend, rolloverModifier])
       
        sciChartSurface?.chartModifiers = groupModifier
    }
NOTE: The ChartModifier API not only allows you to add zoom, panning and dragging behaviours to the chart, but also tooltipslegends and more. You can also create your own custom modifiers. For more information, read about our powerful ChartModifier API here.

The legend requires that DataSeries are named. So, we adjust the code from Tutorial 03 - Adding Series to the chart to give series a name:

Naming DataSeries for the Legend
Copy Code
    func createDataSeries(){
        // Init line data series
        lineDataSeries = SCIXyDataSeries(xType: .double, yType: .double)
        lineDataSeries.seriesName = "Line Series"
        for i in 0..<500{
            lineDataSeries.appendX(SCIGeneric(i), y: SCIGeneric(sin(Double(i))*0.01))
        }
       
        // Init scatter data series
        scatterDataSeries = SCIXyDataSeries(xType: .double, yType: .double)
        scatterDataSeries.seriesName = "Scatter Series"
        for i in 0..<500{
            scatterDataSeries.appendX(SCIGeneric(i), y: SCIGeneric(cos(Double(i))*0.01))
        }
    }

 

As result, the legend with data series names. You can now select/deselect data series - the result will be appearing and dissapearing renderable series on your screen

 

See Also