Search code examples
javascriptvictory-charts

Victory Charts -- Break Line


Is it possible to break the plot line in a Victory Chart? I could do it by making two different plot lines of the same color, but I'm looking for an automatic way. I have data with a lot of zeroes, and I just want the curves. If I remove the zero values, the lines connect.

I have this. The lines connect at zero, underneath the axes origin: enter image description here

I want this: enter image description here


Solution

  • I used this function to create multiple data lines from an array with zeroes:

    export function breakLine(buffer: any[], startTime: any) {
      let zeroCounter = 0;
      let signalCounter = 0;
      let curr: any[] = [];
      let result: any[] = [];
      buffer.forEach((value: any) => {
        // any value less than 20 will be considered a zero
        if (value.result[0] < 20) {
          zeroCounter++;
        } else {
          curr = [
            ...curr,
            {
              x: value.time - startTime,
              y: value.result[0],
            },
          ];
          signalCounter++;
          zeroCounter = 0;
        }
    
        // break line on 3 or more consecutive zeroes
        if (zeroCounter >= 3 && signalCounter > 0) {
          result = add(curr, result);
          zeroCounter = 0;
          signalCounter = 0;
          curr = [];
        }
      });
      // remaining in current after loop
      result = add(curr, result);
      return result.filter((plot: any) => plot.length > 0);
    }
    

    The I map a line for each nested array:

    line1.map((data) =>
            <VictoryLine
            style={{
                data: { stroke: '#0000FF' },
                parent: { border: '1px solid #ccc' },
            }}
            data={data}
        />)