-
Notifications
You must be signed in to change notification settings - Fork 0
Implement a component that draws a chart #44
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Artem-Semenov-dev
wants to merge
7
commits into
share-price-chart
Choose a base branch
from
charts-rendering
base: share-price-chart
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
6e1c6ac
Implement the container component for the chart.
Artem-Semenov-dev 1894fc0
Implement the extensions for the `DrawScope` type.
Artem-Semenov-dev bb22404
Implement the `Chart` component.
Artem-Semenov-dev e633d22
Add documentation for the parameters of the `ShareInfo` and `ChartCon…
Artem-Semenov-dev 942b07f
Move `Chart` component to the `component` package.
Artem-Semenov-dev 302cf6e
Configure the `headlineLarge` text style for the `MaterialTheme`.
Artem-Semenov-dev 4a6955a
Remove dots from the parameter section of documentation for the `mapT…
Artem-Semenov-dev File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
110 changes: 110 additions & 0 deletions
110
client/src/main/kotlin/io/spine/examples/shareaware/client/DrawScopeExts.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,110 @@ | ||
| /* | ||
| * Copyright 2023, TeamDev. All rights reserved. | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Redistribution and use in source and/or binary forms, with or without | ||
| * modification, must retain the above copyright notice and the following | ||
| * disclaimer. | ||
| * | ||
| * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | ||
| * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | ||
| * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | ||
| * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | ||
| * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | ||
| * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | ||
| * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | ||
| * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | ||
| * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | ||
| * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | ||
| * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
| */ | ||
|
|
||
| package io.spine.examples.shareaware.client | ||
|
|
||
| import androidx.compose.ui.geometry.Offset | ||
| import androidx.compose.ui.graphics.Color | ||
| import androidx.compose.ui.graphics.drawscope.DrawScope | ||
| import org.jetbrains.skia.Point | ||
|
|
||
| /** | ||
| * Draws an Y axis inside the drawing area. | ||
| * | ||
| * @param color the color of the axis line | ||
| * @param axisWidth the width of the axis line | ||
| */ | ||
| public fun DrawScope.drawYAxis( | ||
| color: Color, | ||
| axisWidth: Float = 2f | ||
| ) { | ||
| drawLine( | ||
| color = color, | ||
| start = Offset(0f, 0f), | ||
| end = Offset(0f, size.height), | ||
| strokeWidth = axisWidth | ||
| ) | ||
| } | ||
|
|
||
| /** | ||
| * Draws an X axis inside the drawing area. | ||
| * | ||
| * @param color the color of the axis line | ||
| * @param axisWidth the width of the axis line | ||
| */ | ||
| public fun DrawScope.drawXAxis( | ||
| color: Color, | ||
| axisWidth: Float = 2f | ||
| ) { | ||
| drawLine( | ||
| color = color, | ||
| start = Offset(0f, size.height - 0f), | ||
| end = Offset(size.width, size.height - 0f), | ||
| strokeWidth = axisWidth | ||
| ) | ||
| } | ||
|
|
||
| /** | ||
| * Maps a list of points from their original range to pixel coordinates within the drawing area. | ||
| * | ||
| * @param points the list of points to map | ||
| */ | ||
| public fun DrawScope.mapToPixelPoints(points: List<Point>): List<Point> { | ||
| val minXValue = points.minOf { it.x } | ||
| val maxXValue = points.maxOf { it.x } | ||
| val minYValue = points.minOf { it.y } | ||
| val maxYValue = points.maxOf { it.y } | ||
| return points.map { | ||
| val x = it.x.mapToDifferentRange( | ||
| inMin = minXValue, | ||
| inMax = maxXValue, | ||
| outMin = 0f, | ||
| outMax = size.width | ||
| ) | ||
| val y = it.y.mapToDifferentRange( | ||
| inMin = minYValue, | ||
| inMax = maxYValue, | ||
| outMin = size.height, | ||
| outMax = 0f | ||
| ) | ||
| Point(x, y) | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Remaps a value from one range to another. | ||
| * | ||
| * @param inMin The minimum value of the original range | ||
| * @param inMax The maximum value of the original range | ||
| * @param outMin The minimum value of the target range | ||
| * @param outMax The maximum value of the target range | ||
| */ | ||
| private fun Float.mapToDifferentRange( | ||
| inMin: Float, | ||
| inMax: Float, | ||
| outMin: Float, | ||
| outMax: Float | ||
| ): Float = (this - inMin) * (outMax - outMin) / (inMax - inMin) + outMin | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
70 changes: 70 additions & 0 deletions
70
client/src/main/kotlin/io/spine/examples/shareaware/client/component/Chart.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,70 @@ | ||
| /* | ||
| * Copyright 2023, TeamDev. All rights reserved. | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Redistribution and use in source and/or binary forms, with or without | ||
| * modification, must retain the above copyright notice and the following | ||
| * disclaimer. | ||
| * | ||
| * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | ||
| * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | ||
| * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | ||
| * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | ||
| * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | ||
| * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | ||
| * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | ||
| * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | ||
| * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | ||
| * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | ||
| * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
| */ | ||
|
|
||
| package io.spine.examples.shareaware.client.component | ||
|
|
||
| import androidx.compose.foundation.Canvas | ||
| import androidx.compose.foundation.layout.fillMaxSize | ||
| import androidx.compose.material.MaterialTheme | ||
| import androidx.compose.runtime.Composable | ||
| import androidx.compose.ui.Modifier | ||
| import androidx.compose.ui.graphics.Path | ||
| import androidx.compose.ui.graphics.drawscope.Stroke | ||
| import io.spine.examples.shareaware.client.drawXAxis | ||
| import io.spine.examples.shareaware.client.drawYAxis | ||
| import io.spine.examples.shareaware.client.mapToPixelPoints | ||
| import org.jetbrains.skia.Point | ||
|
|
||
| /** | ||
| * Draws a chart. | ||
| * | ||
| * @param points chart points | ||
| */ | ||
| @Composable | ||
| public fun Chart(points: List<Point>) { | ||
| val axisColor = MaterialTheme.colors.onSecondary | ||
| val chartLineColor = MaterialTheme.colors.primary | ||
| Canvas( | ||
| modifier = Modifier.fillMaxSize() | ||
| ) { | ||
| drawYAxis(axisColor) | ||
| drawXAxis(axisColor) | ||
| val pixelPoints = mapToPixelPoints(points) | ||
| val path = Path() | ||
| pixelPoints.forEachIndexed { index, point -> | ||
| if (index == 0) { | ||
| path.moveTo(point.x, point.y) | ||
| } else { | ||
| path.lineTo(point.x, point.y) | ||
| } | ||
| } | ||
| drawPath( | ||
| path = path, | ||
| color = chartLineColor, | ||
| style = Stroke(width = 2f) | ||
| ) | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also, it might be better to name the parameters as
originalMinandtargetMin.