Skip to content

Bugs in the code in the Line Chart example in the readme #186

@cristan

Description

@cristan

The code on the Readme:

val pointsData: List<Point> =
    listOf(Point(0f, 40f), Point(1f, 90f), Point(2f, 0f), Point(3f, 60f), Point(4f, 10f))
val xAxisData = AxisData.Builder()
  .axisStepSize(100.dp)
  .backgroundColor(Color.Blue)
  .steps(pointsData.size - 1)
  .labelData { i -> i.toString() }
  .labelAndAxisLinePadding(15.dp)
  .build()

val yAxisData = AxisData.Builder()
  .steps(steps)
  .backgroundColor(Color.Red)
  .labelAndAxisLinePadding(20.dp)
  .labelData { i ->
      val yScale = 100 / steps
      (i * yScale).formatToSinglePrecision()
  }.build()
val lineChartData = LineChartData(
  linePlotData = LinePlotData(
    lines = listOf(
      Line(
        dataPoints = pointsData,
        LineStyle(),
        IntersectionPoint(),
        SelectionHighlightPoint(),
        ShadowUnderLine(),
        SelectionHighlightPopUp()
      )
    ),
  ),
  xAxisData = xAxisData,
  yAxisData = yAxisData,
  gridLines = GridLines(),
  backgroundColor = Color.White
)
LineChart(
  modifier = Modifier
    .fillMaxWidth()
    .height(300.dp),
  lineChartData = lineChartData
)

This has several problems:

Doesn't compile

This doesn't compile:

.labelData { i ->
      val yScale = 100 / steps
      (i * yScale).formatToSinglePrecision()
  }.build()

If you change the 100 into 100f, it does.

Wrong value in y axis (1)

If I correct this, it looks like this:
Image
Like you can see, the y axis implies that the second dot has the value of 100. It does not: it has the value of 90.

Wrong value in y axis (2)

If you then change the third point from Point(2f, 0f) to Point(2f, 10f), it looks like this:

Image

The third value is then implied to be 0. It is not, it has the value of 10.

First number of the X axis can't be read

The 0 on the X axis can't be read as it is overwritten by the red Y axis legend. The previous issues are all related, but this is a new one.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions