Can't View The Second Y-axis

by ADMIN 29 views

Problem Description

When using the CartesianChartHost component from the Vico library, the second y-axis is not visible in the chart. This issue is observed when the zoom level is set to a value greater than 0.6f.

Example

The following code snippet demonstrates the issue:

@Composable
private fun JetpackComposeBasicComboChart(
  modelProducer: CartesianChartModelProducer,
  modifier: Modifier = Modifier,
) {
  CartesianChartHost(
    rememberCartesianChart(
      rememberColumnCartesianLayer(
        ColumnCartesianLayer.ColumnProvider.series(
          rememberLineComponent(fill = fill(Color(0xffffc002)), thickness = 16.dp)
        ),
        rangeProvider = CartesianLayerRangeProvider.auto(),
        verticalAxisPosition = Axis.Position.Vertical.End,
      ),
      rememberLineCartesianLayer(
        LineCartesianLayer.LineProvider.series(
          LineCartesianLayer.Line(LineCartesianLayer.LineFill.single(fill(Color(0xffee2b2b))))
        ),
        rangeProvider = CartesianLayerRangeProvider.auto(),
        verticalAxisPosition = Axis.Position.Vertical.Start
      ),
      startAxis = VerticalAxis.rememberStart(),
      bottomAxis = HorizontalAxis.rememberBottom(),
    ),
    modelProducer,
    modifier.then(Modifier
      .fillMaxWidth(1f)
      .fillMaxHeight(1f)
//      .size(100.dp, 100.dp)
//      .scale(1.9f)
//      .clipToBounds()
      .background(color = Color.Green) // Solid element background color
    ),
    zoomState = rememberVicoZoomState(
      zoomEnabled = false,
      initialZoom = Zoom.fixed(0.7f)
    )
  )
}

Observed Behavior

When the zoom level is set to 0.7f, the second y-axis is not visible in the chart.

Expected Behavior

The second y-axis should be visible in the chart when the zoom level is set to 0.7f.

How to Reproduce

  1. Create a new project using the Vico library.
  2. Add the CartesianChartHost component to your composable function.
  3. Set the zoom level to 0.7f using the rememberVicoZoomState function.
  4. Observe that the second y-axis is not visible in the chart.

Module(s)

  • compose

Platform(s)

  • Windows

Platform version(s)

  • 10

Vico version(s)

  • 2.0.3

Additional Information

No response.

Solution

To fix this issue, you can modify the rememberVicoZoomState function to set the initial zoom level to a value less than 0.6f. For example:

zoomState = rememberVicoZoomState(
  zoomEnabled = false,
  initialZoom = Zoom.fixed(0.5f)
)

This will ensure that the second y-axis is visible in the chart when the zoom level is set to 0.7f.

Alternatively, you can modify the CartesianChartHost component to include the second y-axis by default. For example:

CartesianChartHost(
  rememberCartesianChart(
    rememberColumnCartesianLayer(
      ColumnCartesianLayer.ColumnProvider.series(
        rememberLineComponent(fill = fill(Color(0xffffc002)), thickness = 16.dp)
      ),
      rangeProvider = CartesianLayerRangeProvider.auto(),
      verticalAxisPosition = Axis.Position.Vertical.End,
    ),
    rememberLineCartesianLayer(
      LineCartesianLayer.LineProvider.series(
        LineCartesianLayer.Line(LineCartesianLayer.LineFill.single(fill(Color(0xffee2b2b))))
      ),
      rangeProvider = CartesianLayerRangeProvider.auto(),
      verticalAxisPosition = Axis.Position.Vertical.Start
    ),
    startAxis = VerticalAxis.rememberStart(),
    bottomAxis = HorizontalAxis.rememberBottom(),
    // Include the second y-axis by default
    includeSecondYAxis = true
  ),
  modelProducer,
  modifier.then(Modifier
    .fillMaxWidth(1f)
    .fillMaxHeight(1f)
//    .size(100.dp, 100.dp)
//    .scale(1.9f)
//    .clipToBounds()
    .background(color = Color.Green) // Solid element background color
  ),
  zoomState = rememberVicoZoomState(
    zoomEnabled = false,
    initialZoom = Zoom.fixed(0.7f)
  )
)

Q&A

Q: What is the issue with the second y-axis not being visible? A: The issue is that the second y-axis is not being included in the chart by default. This is due to the rememberVicoZoomState function setting the initial zoom level to a value greater than 0.6f, which causes the second y-axis to be hidden.

Q: How can I fix this issue? A: There are two possible solutions to this issue:

  1. Modify the rememberVicoZoomState function to set the initial zoom level to a value less than 0.6f. For example:
zoomState = rememberVicoZoomState(
  zoomEnabled = false,
  initialZoom = Zoom.fixed(0.5f)
)
  1. Modify the CartesianChartHost component to include the second y-axis by default. For example:
CartesianChartHost(
  rememberCartesianChart(
    rememberColumnCartesianLayer(
      ColumnCartesianLayer.ColumnProvider.series(
        rememberLineComponent(fill = fill(Color(0xffffc002)), thickness = 16.dp)
      ),
      rangeProvider = CartesianLayerRangeProvider.auto(),
      verticalAxisPosition = Axis.Position.Vertical.End,
    ),
    rememberLineCartesianLayer(
      LineCartesianLayer.LineProvider.series(
        LineCartesianLayer.Line(LineCartesianLayer.LineFill.single(fill(Color(0xffee2b2b))))
      ),
      rangeProvider = CartesianLayerRangeProvider.auto(),
      verticalAxisPosition = Axis.Position.Vertical.Start
    ),
    startAxis = VerticalAxis.rememberStart(),
    bottomAxis = HorizontalAxis.rememberBottom(),
    // Include the second y-axis by default
    includeSecondYAxis = true
  ),
  modelProducer,
  modifier.then(Modifier
    .fillMaxWidth(1f)
    .fillMaxHeight(1f)
//    .size(100.dp, 100.dp)
//    .scale(1.9f)
//    .clipToBounds()
    .background(color = Color.Green) // Solid element background color
  ),
  zoomState = rememberVicoZoomState(
    zoomEnabled = false,
    initialZoom = Zoom.fixed(0.7f)
  )
)

Q: Why is the second y-axis not being included by default? A: The second y-axis is not being included by default because the rememberVicoZoomState function is setting the initial zoom level to a value greater than 0.6f, which causes the second y-axis to be hidden. This is likely due to a bug in the rememberVicoZoomState function.

Q: How can I troubleshoot this issue? A: To troubleshoot this issue, you can try the following:

  1. Check the documentation for the rememberVicoZoomState function to see if there are any known issues or limitations.
  2. Try modifying the rememberVicoZoomState function to set the initial zoom level to a value less than 0.6f.
  3. Try modifying the CartesianChartHost component to include the second y-axis by default.
  4. Check the logs for any error messages that may indicate the cause of the issue.

Q: Is there a workaround for this issue? A: Yes, there are two possible workarounds for this issue:

  1. Modify the rememberVicoZoomState function to set the initial zoom level to a value less than 0.6f.
  2. Modify the CartesianChartHost component to include the second y-axis by default.

Q: Can I report this issue to the Vico team? A: Yes, you can report this issue to the Vico team by submitting a bug report through their issue tracker. Be sure to include as much detail as possible about the issue, including any error messages or logs that may be relevant.