Search code examples
iosswiftrefactoringdry

How to refactor this code so only some properties are shared between variables in Swift


I only just began learning swift and wanted to create a simple chart displaying some data.

I am creating a line chart using AnyChart library and there is a series of lines I am plotting to the chart. I noticed that I am repeating pretty much all of the properties. How can I make the below code less dry.

I am creating a line chart using AnyChart library and there is a series of lines I am plotting to the chart. I noticed that I am repeating pretty much all of the properties, the only thing that is changing is the initial variable name.

How can I make produce less code that will take into account the names of the variables intact (series 1, series 2)?

let series1Mapping = set.mapAs(mapping: "{x: 'x', value: 'value'}")        
let series2Mapping = set.mapAs(mapping: "{x: 'x', value: 'value2'}")

        let series1 = chart.line(data: series1Mapping)
        let series2 = chart.line(data: series2Mapping)


        series1.name(name: data.seriesNames[0])
        series1.hovered().markers().enabled(enabled: true)
        series1.hovered().markers()
            .type(type: anychart.enums.MarkerType.CIRCLE)
            .size(size: 4)
        series1.tooltip()
            .position(position: data.position)
            .anchor(anchor: anychart.enums.Anchor.LEFT_CENTER)
            .offsetX(offset: 5)
            .offsetY(offset: 5)

        series2.name(name: data.seriesNames[1])
        series2.hovered().markers().enabled(enabled: true)
        series2.hovered().markers()
            .type(type: anychart.enums.MarkerType.CIRCLE)
            .size(size: 4)
        series2.tooltip()
            .position(position: data.position)
            .anchor(anchor: anychart.enums.Anchor.LEFT_CENTER)
            .offsetX(offset: 5)
            .offsetY(offset: 5)

Solution

  • You can do it like this.. Its just a rough idea ...i don't know what is Series object and mapping that you are doing ... But you can have one function that return series and get parameters to create that series

     func getSeries(number:Int, mapping:String) -> Series {
    
             let series = chart.line(data: set.mapAs(mapping: mapping))
    
            series.name(name: data.seriesNames[number])
            series.hovered().markers().enabled(enabled: true)
            series.hovered().markers()
                .type(type: anychart.enums.MarkerType.CIRCLE)
                .size(size: 4)
            series.tooltip()
                .position(position: data.position)
                .anchor(anchor: anychart.enums.Anchor.LEFT_CENTER)
                .offsetX(offset: 5)
                .offsetY(offset: 5)
    
            return series
    
        }
    

    And then Create Series

      let series1 = getSeries(number:0 , mapping:"{x: 'x', value: 'value'}")
      let series2 = getSeries(number:1 , mapping:"{x: 'x', value: 'value2'}")
    

    if you want to make it more simpler ... you can create mapping string from the number as well

    Thanks