Skip to content

Commit

Permalink
Merge pull request #190 from TelemetryDeck/StackedBarCharts
Browse files Browse the repository at this point in the history
Stacked bar charts
  • Loading branch information
Black-Fox-2022 authored May 28, 2024
2 parents ce1fbd3 + f9a73bf commit a70aac4
Show file tree
Hide file tree
Showing 11 changed files with 348 additions and 15 deletions.
167 changes: 160 additions & 7 deletions Cluster/Chart/BarChart/BarCharTopN.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,169 @@ import Charts
import DataTransferObjects

struct BarChartTopN: View {
let result: TopNQueryResult
let topNQueryResult: TopNQueryResult
let query: CustomQuery

var body: some View {

Chart {
//ForEach(result.rows) { row in
// BarMark(
// x: .value("Date", 4),
// y: .value("Total Count", 5)
// )
//}
ForEach(topNQueryResult.rows, id: \.self) { (row: TopNQueryResultRow) in

ForEach(row.result, id: \.self) { (rowResult: AdaptableQueryResultItem) in

ForEach(query.aggregations ?? [], id: \.self) { (aggregator: Aggregator) in
if let metricValue = getMetricValue(rowResult: rowResult){
getBarMark(
timeStamp: row.timestamp,
name: getAggregatorName(aggregator: aggregator),
metricValue: metricValue,
metricName: getMetricName(rowResult: rowResult)
)
}
}

}

}
}
.chartForegroundStyleScale(range: Color.chartColors)

}

// swiftlint:disable cyclomatic_complexity
func granularity() -> Calendar.Component{
switch query.granularity {
case .all:
.month
case .none:
.month
case .second:
.hour
case .minute:
.hour
case .fifteen_minute:
.hour
case .thirty_minute:
.hour
case .hour:
.hour
case .day:
.day
case .week:
.weekOfYear
case .month:
.month
case .quarter:
.quarter
case .year:
.year
}
}
// swiftlint:enable cyclomatic_complexity

func getBarMark(timeStamp: Date, name: String, metricValue: Double, metricName: String) -> some ChartContent {
return BarMark(
x: .value("Date", timeStamp, unit: granularity()),
y: .value(name, metricValue)
)
.foregroundStyle(by: .value(getDimensionName(from: query.dimension) ?? "Not found", metricName))
}

func getDimensionName(from: DimensionSpec?) -> String? {
switch from {
case .default(let defaultDimensionSpec):
defaultDimensionSpec.outputName
case .extraction(let extractionDimensionSpec):
extractionDimensionSpec.outputName
case .none:
nil
}
}

func getMetricName(rowResult: AdaptableQueryResultItem) -> String{
let dimensionName = getDimensionName(from: query.dimension) ?? "Not found"
let metricName = rowResult.dimensions[dimensionName] ?? "Not found"
return metricName
}

// swiftlint:disable cyclomatic_complexity
// swiftlint:disable function_body_length
func getAggregatorName(aggregator: Aggregator) -> String {
switch aggregator {
case .count(let a):
a.name
case .cardinality(let a):
a.name
case .longSum(let a):
a.name
case .doubleSum(let a):
a.name
case .floatSum(let a):
a.name
case .doubleMin(let a):
a.name
case .doubleMax(let a):
a.name
case .floatMin(let a):
a.name
case .floatMax(let a):
a.name
case .longMin(let a):
a.name
case .longMax(let a):
a.name
case .doubleMean(let a):
a.name
case .doubleFirst(let a):
a.name
case .doubleLast(let a):
a.name
case .floatFirst(let a):
a.name
case .floatLast(let a):
a.name
case .longFirst(let a):
a.name
case .longLast(let a):
a.name
case .stringFirst(let a):
a.name
case .stringLast(let a):
a.name
case .doubleAny(let a):
a.name
case .floatAny(let a):
a.name
case .longAny(let a):
a.name
case .stringAny(let a):
a.name
case .thetaSketch(let a):
a.name
case .filtered(let a):
getAggregatorName(aggregator: a.aggregator)
}
}
// swiftlint:enable cyclomatic_complexity
// swiftlint:enable function_body_length

func getMetricValue(rowResult: AdaptableQueryResultItem) -> Double? {
guard let metric = query.metric, let metricName = getMetricName(metric: metric) else {
return nil
}
let value = rowResult.metrics[metricName]
return value
}

func getMetricName(metric: TopNMetricSpec) -> String? {
switch metric {
case .numeric(let numericTopNMetricSpec):
return numericTopNMetricSpec.metric
case .inverted(let invertedTopNMetricSpec):
return getMetricName(metric: invertedTopNMetricSpec.metric)
default:
return nil
}
}

}
8 changes: 1 addition & 7 deletions Cluster/Chart/BarChart/ClusterBarChart.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,9 @@ struct ClusterBarChart: View {
} else {
Text("Mismatch in query type and result type 1")
}
/*case .groupBy:
if case let .groupBy(result) = result {
BarChartGroupBy(result: result)
} else {
Text("Mismatch in query type and result type 2")
}*/
case .topN:
if case let .topN(result) = result {
BarChartTopN(result: result)
BarChartTopN(topNQueryResult: result, query: query)
} else {
Text("Mismatch in query type and result type 2")
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"colors" : [
{
"color" : {
"color-space" : "srgb",
"components" : {
"alpha" : "1.000",
"blue" : "0.141",
"green" : "0.749",
"red" : "0.984"
}
},
"idiom" : "universal"
}
],
"info" : {
"author" : "xcode",
"version" : 1
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"colors" : [
{
"color" : {
"color-space" : "srgb",
"components" : {
"alpha" : "1.000",
"blue" : "0.600",
"green" : "0.827",
"red" : "0.204"
}
},
"idiom" : "universal"
}
],
"info" : {
"author" : "xcode",
"version" : 1
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"colors" : [
{
"color" : {
"color-space" : "srgb",
"components" : {
"alpha" : "1.000",
"blue" : "0.933",
"green" : "0.827",
"red" : "0.133"
}
},
"idiom" : "universal"
}
],
"info" : {
"author" : "xcode",
"version" : 1
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"colors" : [
{
"color" : {
"color-space" : "srgb",
"components" : {
"alpha" : "1.000",
"blue" : "0.976",
"green" : "0.475",
"red" : "0.910"
}
},
"idiom" : "universal"
}
],
"info" : {
"author" : "xcode",
"version" : 1
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"colors" : [
{
"color" : {
"color-space" : "srgb",
"components" : {
"alpha" : "1.000",
"blue" : "0.980",
"green" : "0.647",
"red" : "0.376"
}
},
"idiom" : "universal"
}
],
"info" : {
"author" : "xcode",
"version" : 1
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"colors" : [
{
"color" : {
"color-space" : "srgb",
"components" : {
"alpha" : "1.000",
"blue" : "0.208",
"green" : "0.902",
"red" : "0.639"
}
},
"idiom" : "universal"
}
],
"info" : {
"author" : "xcode",
"version" : 1
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"colors" : [
{
"color" : {
"color-space" : "srgb",
"components" : {
"alpha" : "1.000",
"blue" : "0.522",
"green" : "0.443",
"red" : "0.984"
}
},
"idiom" : "universal"
}
],
"info" : {
"author" : "xcode",
"version" : 1
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"colors" : [
{
"color" : {
"color-space" : "srgb",
"components" : {
"alpha" : "1.000",
"blue" : "0.973",
"green" : "0.741",
"red" : "0.220"
}
},
"idiom" : "universal"
}
],
"info" : {
"author" : "xcode",
"version" : 1
}
}
Loading

0 comments on commit a70aac4

Please sign in to comment.