Search code examples
javascriptcanvasjs

Change xValue exhibition


I have this plot, where I put some markers (stripLines) on minimun and maximum values. I´d like to show the xValue only on the markers, to make the chart more readable.

enter image description here

In this plot I'm using an interval of 50 and my data for xAxis is not set properly. What I wish to do is to show the value on xAxis only where the markers show up. Is there any property that does that? I can't find it on the documentaion.

Here is the code where I generate the chart

const options = {
  theme: "light2", // "light1", "dark1", "dark2"
  animationEnabled: true,
  zoomEnabled: false,
  toolTip:{
    enabled: false,
    },
  axisX: [{
    crosshair: { 
    enabled: true
    }, 
    stripLines:[
    {                
      value: marker[0],
      thickness: 2,
      label: ""
    },
    {                
      value: marker[1],
      thickness: 2,
      label: ""
    },
    ],  
    //title: "X title 1",
    //margin: 20,
    valueFormatString: "DD/MMM/YY" ,
    labelMaxWidth: 70,
    //labelWrap: true,
    interval: 50,
    titleFontColor:"#7680B2",
    lineColor:"#7680B2",
    tickColor:"#7680B2",
    labelFontColor:"#7680B2",
    lineThickness: 2,
  },
  {
    //title: "X title 2",
    margin: 0,
    valueFormatString: "DD/MMM/YY" ,
    //zlabelWrap: true,
    interval: 50,
    titleFontColor:"#62D2A9",
    lineColor:"#62D2A9",
    tickColor:"#62D2A9",
    labelFontColor:"#62D2A9",
    lineThickness: 2,

  }],
  axisY: {
    includeZero: false
  },
  data: [
    {
    color:"#7680B2",
    type: "spline",
    axisXIndex: 0,
    xValueType: "dateTime",

    dataPoints: dataPoints,
  },
    {
    color:"#62D2A9",
    type: "spline",
    axisXIndex: 1,
    xValueType: "dateTime",

    dataPoints: dataPoints2,
  },

]
}
  return (
    <div className="ChartWithZoom">
      <h1>Longe do pico</h1>
      <CanvasJSChart options = {options} 
      /* onRef={ref => this.chart = ref} */
      />
    </div>
  );


Solution

  • I think I found a workaround, there is a function that handles that situation, labelFormatter: function ( e ), this is my solution:

    var  chart =  new  CanvasJS.Chart("container",
    {
     .
     .
      axisX:{
        interval: 1,
        intervalType: 'day',
        labelFormatter: function ( e ) {
          let data = CanvasJSReact.CanvasJS.formatDate(e.value, "DD/MMM/YY");
          
          //marker is an array that I get the dates for the marker
          let minMarker = CanvasJSReact.CanvasJS.formatDate(marker[0], "DD/MMM/YY");
          let maxMarker = CanvasJSReact.CanvasJS.formatDate(marker[1], "DD/MMM/YY");
          if ( data === minMarker || data === maxMarker ) {
            return CanvasJSReact.CanvasJS.formatDate(e.value, "DD/MMM/YY");
          } else {
            return "";
        }
    };  
      },
     .
     .
    });
    chart.render();
    
    e: { // parameter sent to function
           chart,
           axis,
           value,
           label
     }

    This is the result: enter image description here