Skip to content

Commit

Permalink
v1.0 initial release
Browse files Browse the repository at this point in the history
  • Loading branch information
ellsworthrw committed Jun 13, 2022
1 parent 7445a1e commit 10c12ad
Show file tree
Hide file tree
Showing 29 changed files with 128 additions and 124 deletions.
4 changes: 3 additions & 1 deletion app/src/main/java/com/diamondedge/chartapp/ui/Previews.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import com.diamondedge.charts.Charts
import com.diamondedge.charts.DefaultData
import com.diamondedge.charts.RandomData
import com.diamondedge.charts.compose.BarChart
Expand All @@ -32,7 +33,8 @@ private fun BarChartHorizontalPreview() {
modifier = Modifier
.fillMaxWidth()
.height(300.dp),
isVertical = false
isVertical = false,
legendPosition = Charts.LEGEND_RIGHT
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package com.diamondedge.chartapp.ui

import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.tooling.preview.Preview
Expand Down Expand Up @@ -41,7 +42,8 @@ private fun BarChartHorizontalPreview() {
RandomData(DefaultData.SIMPLE_SERIES, 3),
modifier = Modifier
.fillMaxWidth()
.height(300.dp),
.height(300.dp)
.padding(horizontal = 8.dp),
isVertical = false
)
}
Expand Down
2 changes: 1 addition & 1 deletion charts/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ tasks {
}

extra["artifactID"] = "charts-android"
extra["artifactVersion"] = "0.9.2"
extra["artifactVersion"] = "1.0.0"
extra["libraryName"] = "Diamond Charts"
extra["libraryDescription"] = "Diamond Charts: charting library for Android Jetpack Compose"
extra["gitUrl"] = "https://github.com/ellsworthrw/DiamondCharts"
Expand Down
6 changes: 1 addition & 5 deletions charts/src/main/java/com/diamondedge/charts/Annotation.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,7 @@
package com.diamondedge.charts

class Annotation(val text: String, val x: Int, val y: Int) : ChartObject() {
private var foreground = Color.defaultTextColor

fun setForeground(c: Long) {
foreground = c
}
var foreground = Color.defaultTextColor

override fun draw(g: GraphicsContext) {
val fm = g.fontMetrics
Expand Down
3 changes: 3 additions & 0 deletions charts/src/main/java/com/diamondedge/charts/CalculatedData.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ import com.diamondedge.fn.Statistics

import java.util.Calendar

/**
* Beta: subject to change.
*/
class CalculatedData(functionType: Int = 0, periodStyle: Int = ALL_POINTS, private val ddata: ChartData) :
DefaultData(functionType, XY_SERIES) {

Expand Down
2 changes: 1 addition & 1 deletion charts/src/main/java/com/diamondedge/charts/Chart.kt
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ abstract class Chart(override val data: ChartData) : ChartObject() {
*
* @see .setLabelType
*/
var isHotspotsAvailable: Boolean
private var isHotspotsAvailable: Boolean
get() = hotspots != null
set(createHotspots) = if (createHotspots)
hotspots = ArrayList()
Expand Down
2 changes: 1 addition & 1 deletion charts/src/main/java/com/diamondedge/charts/ChartObject.kt
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ abstract class ChartObject {

abstract fun draw(g: GraphicsContext)

open fun hitTest(x: Int, y: Int): Hotspot? {
internal open fun hitTest(x: Int, y: Int): Hotspot? {
return null
}
}
17 changes: 7 additions & 10 deletions charts/src/main/java/com/diamondedge/charts/Charts.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ package com.diamondedge.charts

open class Charts(width: Float, height: Float, legendPosition: Int = LEGEND_NONE) {

internal val objectCount: Int
val count: Int
get() = charts.size

private val firstWithAxis: Chart?
Expand All @@ -23,12 +23,15 @@ open class Charts(width: Float, height: Float, legendPosition: Int = LEGEND_NONE

var backgroundColor = Color.defaultBackgroundColor
var legend: Legend? = null
internal set

var legendPosition: Int = 0
var vertAxis: Axis? = null
internal set
var horizontalAxis: Axis? = null
var charts = ArrayList<ChartObject>()
var gridLines: GridLines = GridLines()
internal set
private val charts = ArrayList<ChartObject>()
val gridLines = GridLines()
private val rolloverLabel: Annotation? = null
private val rolloverSpot: Hotspot? = null

Expand Down Expand Up @@ -63,11 +66,6 @@ open class Charts(width: Float, height: Float, legendPosition: Int = LEGEND_NONE
installAxes(obj)
obj.setupAxis()
}
//todo remove
if (obj is Chart) {
// setupHotspots();
obj.isHotspotsAvailable = true
}
return obj
}

Expand Down Expand Up @@ -208,8 +206,7 @@ open class Charts(width: Float, height: Float, legendPosition: Int = LEGEND_NONE
log.d { "chartWidth: ${chartBounds.width} contWidth: ${d.width} leftMargin: $leftMargin rightMargin: $rightMargin vertAxisWidth: $vertAxisWidth vertAxis2Width: $vertAxis2Width" }
if (legendPosition != LEGEND_NONE) {
if (legend == null)
legend = Legend()
legend!!.setGraph(this)
legend = Legend(this)
val legendSize = legend!!.getSize(g)
if (legendPosition == LEGEND_RIGHT)
chartBounds.width -= legendSize!!.width + 5
Expand Down
50 changes: 31 additions & 19 deletions charts/src/main/java/com/diamondedge/charts/Color.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package com.diamondedge.charts

import java.lang.Long.max
import kotlin.math.round

object Color {
const val white = 0xffFFFFFF
const val darkDarkGray = 0xff5A5A5AL
Expand Down Expand Up @@ -33,31 +36,40 @@ object Color {
var defaultTextColor = black
var defaultBackgroundColor = white

fun getRed(color: Long): Long {
return color shr 16 and 255
fun scale(color: Long, scale: Double): Long {
return create(color.alpha, scaleIt(color.red, scale), scaleIt(color.green, scale), scaleIt(color.blue, scale))
}

fun getGreen(color: Long): Long {
return color shr 8 and 255
}
private fun scaleIt(color: Long, scaleBy: Double) = max(255L, round(color * scaleBy).toLong())

fun getBlue(color: Long): Long {
return color and 255
fun create(r: Long, g: Long, b: Long): Long {
return (0xffL shl 24 or (r and 255 shl 16) or (g and 255 shl 8) or (b and 255 shl 0))
}

fun getAlpha(color: Long): Long {
return color shr 24 and 255
fun create(alpha: Long, r: Long, g: Long, b: Long): Long {
return (alpha shl 24 or (r and 255 shl 16) or (g and 255 shl 8) or (b and 255 shl 0))
}
}

fun brighter(color: Long): Long {
return color
}
val Long.alpha
get() = this shr 24 and 255

fun transparent(color: Long): Long {
return (color and 0xffffff) or 0x77000000
}
val Long.red
get() = this shr 16 and 255

fun create(r: Long, g: Long, b: Long): Long {
return (0xffL shl 24 or (r and 255 shl 16) or (g and 255 shl 8) or (b and 255 shl 0))
}
}
val Long.green
get() = this shr 8 and 255

val Long.blue
get() = this and 255

fun Long.withAlpha(alpha: Long) = (this and 0xffffff) or (alpha shl 24)

val Long.transparent
get() = (this and 0xffffff) or 0x77000000

val Long.brighter
get() = Color.scale(this, 1.25)

val Long.darker
get() = Color.scale(this, .75)
1 change: 1 addition & 0 deletions charts/src/main/java/com/diamondedge/charts/DateAxis.kt
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import kotlin.time.Duration.Companion.days
class DateAxis : Axis() {

var tickLabelDateFormat: DateFormat = dateFormat
internal set

/** Applies the given pattern to the SimpleDateFormat object used in formatting the
* labels next to each major tick mark.
Expand Down
38 changes: 11 additions & 27 deletions charts/src/main/java/com/diamondedge/charts/DateUtil.kt
Original file line number Diff line number Diff line change
Expand Up @@ -18,62 +18,51 @@ object DateUtil {
const val ONE_DAY = 1.0
const val ONE_WEEK = 7.0
const val ONE_MONTH = 30.417
const val ONE_YEAR = 365.0
const val ONE_YEAR = 365.25

private var zoneOffset = 0
private val cal = Calendar.getInstance()
private const val millisPerDay = (24 * 60 * 60 * 1000).toDouble()

fun now(): Double {
return toDouble(Date())
}

/** returns a double representing the number of days since Jan 1, 1970
* Note: the fractional part represents the time of day in relation to
* current timezone.
/** returns a double representing the number of days since "the epoch" i.e. Jan 1, 1970
* Note: the fractional part represents the time of day in relation to GMT.
*/
fun toDouble(date: Date?): Double {
return if (date == null) 0.0 else (date.time + zoneOffset) / millisPerDay
// Jan 1, 1970 = 0
return if (date == null) 0.0 else (date.time) / millisPerDay
}

/** returns a double representing the number of days since Jan 1, 1970
* Note: the fractional part represents the time of day in relation to
* current timezone.
* Note: the fractional part represents the time of day in relation to GMT.
*/
fun toDouble(c: Calendar?): Double {
return if (c == null) 0.0 else toDouble(c.time)
// Jan 1, 1970 = 25569 in VB and 0 in Java
}

/** returns a double representing the number of days since Jan 1, 1970
* Note: the fractional part represents the time of day in relation to
* current timezone.
* Note: the fractional part represents the time of day in relation to GMT.
*/
fun toDouble(timeMillis: Long): Double {
return (timeMillis + zoneOffset) / millisPerDay
return timeMillis / millisPerDay
}

/** returns a date for the given day since Jan 1, 1970
* Note: the fractional part represents the time of day in relation to
* current timezone.
* Note: the fractional part represents the time of day in relation to GMT.
*/
fun toDate(days: Double): Date {
// date = 0 days since Jan 1, 1970
return Date(Math.round(days * millisPerDay - zoneOffset))
return Date(Math.round(days * millisPerDay))
}

/** returns a date for the given day since Jan 1, 1970
* Note: the fractional part represents the time of day in relation to
* current timezone.
/** returns number of milliseconds since the standard base time known as "the epoch", namely January 1, 1970, 00:00:00 GMT.
*/
fun toMillis(date: Double): Long {
// date = 0 days since Jan 1, 1970
return (date * millisPerDay).toLong()
}

fun setCalendar(cal: Calendar, days: Double) {
cal.timeInMillis = Math.round(days * millisPerDay - zoneOffset)
cal.timeInMillis = Math.round(days * millisPerDay)
}

fun clearCalBelow(cal: Calendar, calendarField: Int) {
Expand Down Expand Up @@ -113,11 +102,6 @@ object DateUtil {
}

fun durationString(duration: Double): String {
// val millis = toMillis(duration)
return duration.days.toString()
}

init {
zoneOffset = cal.get(Calendar.ZONE_OFFSET) + cal.get(Calendar.DST_OFFSET)
}
}
2 changes: 1 addition & 1 deletion charts/src/main/java/com/diamondedge/charts/DateXYGraph.kt
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,6 @@ class DateXYGraph(data: ChartData, drawLine: Boolean = true, fillArea: Boolean =
}

override fun toString(): String {
return "DateGraph[" + toStringParam() + "]"
return "DateXYGraph[" + toStringParam() + "]"
}
}
2 changes: 1 addition & 1 deletion charts/src/main/java/com/diamondedge/charts/Dimension.kt
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
package com.diamondedge.charts

class Dimension(var width: Int, var height: Int)
class Dimension(val width: Int, val height: Int)
5 changes: 2 additions & 3 deletions charts/src/main/java/com/diamondedge/charts/Draw.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,15 @@ object Draw {
private var colors: ArrayList<Long>? = null
internal var defaultFont = Font.Default

internal fun setFillAttributes(g: GraphicsContext, attr: GraphicAttributes?) {
private fun setFillAttributes(g: GraphicsContext, attr: GraphicAttributes?) {
if (attr == null)
return
if (attr.fill) {
g.color = attr.color
}
}

internal fun setBorderAttributes(g: GraphicsContext, attr: GraphicAttributes?): Boolean {
private fun setBorderAttributes(g: GraphicsContext, attr: GraphicAttributes?): Boolean {
if (attr == null)
return false
if (attr.drawBorder) {
Expand Down Expand Up @@ -399,7 +399,6 @@ object Draw {
}
*/

@JvmStatic
fun getColor(index: Int): Long {
val c: Long
val nColors: Int
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,4 @@ class GraphicAttributes(
var fill: Boolean = true,
var drawBorder: Boolean = true,
var borderColor: Long = Color.defaultTextColor
) {
var draw3D: Boolean = false
internal var flags = 0
}
)
10 changes: 9 additions & 1 deletion charts/src/main/java/com/diamondedge/charts/GraphicsContext.kt
Original file line number Diff line number Diff line change
Expand Up @@ -357,7 +357,7 @@ interface GraphicsContext {
fun drawString(str: String, x: Int, y: Int)

/**
* Draws the text given by the specified string, using this
* Draws the text given by the specified string inside the bounding box specified by *width*, using this
* graphics context's current font and color. The baseline of the
* leftmost character is at position (*x*,&nbsp;*y*) in this
* graphics context's coordinate system. The string will be truncated
Expand Down Expand Up @@ -404,7 +404,15 @@ interface GraphicsContext {
*/
fun getImageSize(image: Any): Pair<Int, Int>

/**
* The `gradient` object will be used to stroke a
* path or fill a shape during the rendering process
*/
fun applyGradient(gradient: Gradient, alpha: Float = 1f)

/**
* Remove the previously applied `gradient`
*/
fun clearGradient()

fun save()
Expand Down
Loading

0 comments on commit 10c12ad

Please sign in to comment.