I have 3 stops at 0.4, 0.6 and 0.8 (red, green, blue). The maximum is 100 (for percent)
If I set the data as 50, I expect it to be green but it seems to be a mix of the green and red. It's only when I get to 60 that it's bright green that I expect.
Anyway to remove the gradient?
https://jsfiddle.net/sy3r2hj7/1/
var gaugeOptions = {
chart: {
type: 'solidgauge'
},
title: null,
pane: {
center: ['50%', '85%'],
size: '140%',
startAngle: -90,
endAngle: 90,
background: {
backgroundColor: (Highcharts.theme && Highcharts.theme.background2) || '#EEE',
innerRadius: '60%',
outerRadius: '100%',
shape: 'arc'
}
},
tooltip: {
enabled: false
},
// the value axis
yAxis: {
stops: [
[0.40, '#ff0000'],
[0.60, '#00ff00'], // yellow
[0.80, '#0000ff'] // red
],
lineWidth: 0,
minorTickInterval: null,
tickAmount: 2,
title: {
y: -70
},
labels: {
y: 16
}
},
plotOptions: {
solidgauge: {
dataLabels: {
y: 5,
borderWidth: 0,
useHTML: true
}
}
}
};
// The speed gauge
var chartSpeed = Highcharts.chart('container-speed', Highcharts.merge(gaugeOptions, {
yAxis: {
min: 0,
max: 100,
title: {
text: 'Speed'
}
},
credits: {
enabled: false
},
series: [{
name: 'Speed',
data: [60],
dataLabels: {
format: '<div style="text-align:center"><span style="font-size:25px;color:' +
((Highcharts.theme && Highcharts.theme.contrastTextColor) || 'black') + '">{y}</span><br/>' +
'<span style="font-size:12px;color:silver">km/h</span></div>'
},
tooltip: {
valueSuffix: ' km/h'
}
}]
}));
Instead of using stops property, you can change the color of the point programmatically, based on some condition:
events: {
render: function() {
var point = this.series[0].points[0];
if (point.y < 40) {
point.graphic.attr({
fill: '#ff0000'
})
} else if (point.y < 60) {
point.graphic.attr({
fill: '#00ff00'
})
} else {
point.graphic.attr({
fill: '#0000ff'
})
}
}
}
Live demo: https://jsfiddle.net/BlackLabel/fw594Lmg/
Related
I'm trying to use values from one class/scrren to multiply with another class screen but keep getting 0.
I know i probably do not carry the variable properly from one class to another but seem to have tried almost everything so if I dont get nontype error I just get 0 value when multiplying anything, like I would carry 0 from my second screen in the values which i have to input in the second screen. Any ideas? as Im brand new on kivy
.py
class SecondWindow(Screen):
ageInput = NumericProperty()
weightInput = NumericProperty()
heightInput = NumericProperty()
def info(self):
age = self.ids.ageInput.text
ageVal = int(age)
height = self.ids.heightInput.text
heightVal = int(height)
weight = self.ids.weightInput.text
weightVal = int(weight)
add = int(age) + int(height) - int(weight)
self.ids.label.text = str(add)
addVal = self.ids.label.text
print (addVal)
return ageVal, heightVal, weightVal
class ThirdWindow(Screen):
male = ObjectProperty()
female = ObjectProperty()
second = ObjectProperty()
def bmrCalc(self, button, weight):
choice = self.ids.lbl.text
if choice == "":
self.ids.lbl.text = button
self.weight = self.second.weightInput
if self.ids.lbl.text == "male":
m = 66.5
mw = 13.75 * int(self.second.weightInput)
mh = 5.003 * int(self.second.heightInput)
ma = 6.75 * int(self.second.ageInput)
print(mw)
.kv file
<SecondWindow>:
id: second
name: "second"
age: ageInput
hheight: heightInput
weight: weightInput
lbl1: label
FloatLayout:
canvas.before:
Color:
rgba: 255, 0, 0, 1
Rectangle:
# self here refers to the widget i.e FloatLay
pos: self.pos
size: self.size
Label:
text: "Calorie Intake Calculator"
font_size: 35
pos_hint: {"x": 0.3, "top": 1}
size_hint: 0.35, 0.1
Label:
text: "Enter your age"
font_size: 30
pos_hint: {"x": 0.1, "y": 0.8}
size_hint: 0.3, 0.1
TextInput:
id: ageInput
font_size: 15
multiline: False
pos_hint: {"x": 0.5 , "y":0.82}
size_hint:0.3, 0.05
Label:
text: "Enter your height(cm)"
font_size: 30
pos_hint: {"x": 0.1, "y": 0.6}
size_hint: 0.3, 0.1
TextInput:
id: heightInput
font_size: 15
multiline: False
pos_hint: {"x": 0.5 , "y":0.62}
size_hint:0.3, 0.05
Label:
text: "Enter your weight(kg)"
font_size: 30
pos_hint: {"x": 0.1, "y": 0.4}
size_hint: 0.3, 0.1
TextInput:
id: weightInput
font_size: 15
multiline: False
pos_hint: {"x": 0.5 , "y":0.42}
size_hint:0.3, 0.05
Button:
pos_hint: {"x":0.35, "y": 0.3}
size_hint: 0.3, 0.05
text: "Submit"
on_press:
root.info()
app.root.current = "third"
root.manager.transition.direction = "left"
Label:
id: label
text: "text"
font_size: 15
pos_hint: {"x": 0.7, "y": 0.1}
size_hint: 0.3, 0.2
Button:
pos_hint: {"x":0.35, "y": 0.15}
size_hint: 0.3, 0.05
text: "Back"
on_release:
app.root.current = "first"
root.manager.transition.direction = "right"
<ThirdWindow>:
id: third
name: "third"
lbl: lbl
male: male
female: female
FloatLayout:
canvas.before:
Color:
rgba: 153, 0, 0, 1
Rectangle:
# self here refers to the widget i.e FloatLayout
pos: self.pos
size: self.size
Label:
text: "Calorie Intake Calculator"
font_size: 35
pos_hint: {"x": 0.3, "top": 1}
size_hint: 0.35, 0.1
Label:
text: "Choose Your gender"
font_size: 30
pos_hint: {"x": 0.32, "top": 0.85}
size_hint: 0.35, 0.1
Button:
id: male
pos_hint: {"x":0.35, "y": 0.6}
size_hint: 0.3, 0.1
text: "Male"
on_press:
root.bmrCalc("male", "weight")
Button:
id: female
pos_hint: {"x":0.35, "y": 0.5}
size_hint: 0.3, 0.1
text: "Female"
on_press:
root.bmrCalc("female")
Label:
id: lbl
text: ""
font_size: 15
pos_hint: {"x": 0.35, "top": 0.3}
size_hint: 0.35, 0.1
Button:
pos_hint: {"x":0.35, "y": 0.1}
size_hint: 0.3, 0.05
text: "Back"
on_press:
app.root.current = "second"
root.manager.transition.direction = "right"
here are two charts.
and I would like to show the data of object and object2 together in one chart.
Would it be possible?
Chart(db.objects, id: \.self) { object in
LineMark(
x: .value("name", object.name),
y: .value("value", Int(object.value) ?? 0)
)
}
Chart(db.objects2, id: \.self) { object2 in
LineMark(
x: .value("name", object2.name),
y: .value("value", Int(object2.value) ?? 0)
)
}
I just found the way from apple developer website
https://developer.apple.com/documentation/charts/chart/
struct ProfitOverTime {
var date: Date
var profit: Double
}
let departmentAProfit: [ProfitOverTime] = [] // ...
let departmentBProfit: [ProfitOverTime] = [] // ...
var body: some View {
Chart {
ForEach(departmentAProfit) {
LineMark(
x: .value("Date", $0.date),
y: .value("Profit A", $0.profit)
)
.foregroundStyle(.blue)
}
ForEach(departmentBProfit) {
LineMark(
x: .value("Date", $0.date),
y: .value("Profit B", $0.profit)
)
.foregroundStyle(.green)
}
RuleMark(
y: .value("Threshold", 500.0)
)
.foregroundStyle(.red)
}
}
This is an alternative solution. Since the data structure is the same, the data could be one source and displayed using one LineMark.
As an example, dept was added to the model to identify each group of data that represents a single line in the chart:
struct ProfitOverTime {
var dept: String
var date: Date
var profit: Double
}
The data combined into one array:
let data: [ProfitOverTime] = [] // ...
The foreground style is based on the department, that is, each line in the chart.
struct DepartmentChart: View {
var body: some View {
Chart(data) {
LineMark(
x: .value("Date", $0.date),
y: .value("Profit", $0.profit)
series: .value("Department", $0.dept)
)
.foregroundStyle(by: .value("Department", $0.dept))
RuleMark(
y: .value("Threshold", 500.0)
)
.foregroundStyle(.red)
}
.chartForegroundStyleScale(["Profit A": .green, "Profit B": .blue])
}
}
Series was added to identify each line by its color:
The chartForegroundStyleScale is optional since each line will automatically be colored differently. But chartForegroundStyleScale can be used to customize the line colors.
Here I am showing the data for a week. So if you want to show more than data then you can increase the number of entries. So that’s it from my side hope you understand.
Now use MultiLineChartView like this.
Complete Code:
import SwiftUI
import Charts
struct ContentView: View {
let days = ["S", "M", "T", "W", "T", "F", "S"]
let entries1 = [
ChartDataEntry(x: 1, y: 1),
ChartDataEntry(x: 2, y: 2),
ChartDataEntry(x: 3, y: 0),
ChartDataEntry(x: 4, y: 0),
ChartDataEntry(x: 5, y: 0),
ChartDataEntry(x: 6, y: 0),
ChartDataEntry(x: 7, y: 1),
]
let entries2 = [
ChartDataEntry(x: 1, y: 2),
ChartDataEntry(x: 2, y: 3),
ChartDataEntry(x: 3, y: 0),
ChartDataEntry(x: 4, y: 0),
ChartDataEntry(x: 5, y: 0),
ChartDataEntry(x: 6, y: 0),
ChartDataEntry(x: 7, y: 2)
]
var body: some View {
VStack{
Spacer()
MultiLineChartView(entries1: entries1, entries2: entries2, days: days)
.frame(height: 220)
Spacer()
}
}
}
struct MultiLineChartView : UIViewRepresentable {
var entries1 : [ChartDataEntry]
var entries2 : [ChartDataEntry]
var days: [String]
func makeUIView(context: Context) -> LineChartView {
let chart = LineChartView()
return createChart(chart: chart)
}
func updateUIView(_ uiView: LineChartView, context: Context) {
uiView.data = addData()
}
func createChart(chart: LineChartView) -> LineChartView{
chart.chartDescription?.enabled = false
chart.xAxis.drawGridLinesEnabled = false
chart.xAxis.drawLabelsEnabled = true
chart.xAxis.drawAxisLineEnabled = false
chart.xAxis.labelPosition = .bottom
chart.rightAxis.enabled = false
chart.leftAxis.enabled = false
chart.drawBordersEnabled = false
chart.legend.form = .none
chart.xAxis.labelCount = 7
chart.xAxis.forceLabelsEnabled = true
chart.xAxis.granularityEnabled = true
chart.xAxis.granularity = 1
chart.xAxis.valueFormatter = CustomChartFormatter(days: days)
chart.data = addData()
return chart
}
func addData() -> LineChartData{
let data = LineChartData(dataSets: [
//Schedule Trips Line
generateLineChartDataSet(dataSetEntries: entries1, color: UIColor(Color(#colorLiteral(red: 0.6235294118, green: 0.7333333333, blue: 0.3568627451, alpha: 1))), fillColor: UIColor(Color(#colorLiteral(red: 0, green: 0.8134518862, blue: 0.9959517121, alpha: 1)))),
//Unloadings Line
generateLineChartDataSet(dataSetEntries: entries2, color: UIColor(Color(#colorLiteral(red: 0.003921568627, green: 0.231372549, blue: 0.431372549, alpha: 1))), fillColor: UIColor(Color(#colorLiteral(red: 0.4745098054, green: 0.8392156959, blue: 0.9764705896, alpha: 1))))
])
return data
}
func generateLineChartDataSet(dataSetEntries: [ChartDataEntry], color: UIColor, fillColor: UIColor) -> LineChartDataSet{
let dataSet = LineChartDataSet(entries: dataSetEntries, label: "")
dataSet.colors = [color]
dataSet.mode = .cubicBezier
dataSet.circleRadius = 5
dataSet.circleHoleColor = UIColor(Color(#colorLiteral(red: 0.003921568627, green: 0.231372549, blue: 0.431372549, alpha: 1)))
dataSet.fill = Fill.fillWithColor(fillColor)
dataSet.drawFilledEnabled = true
dataSet.setCircleColor(UIColor.clear)
dataSet.lineWidth = 2
dataSet.valueTextColor = color
dataSet.valueFont = UIFont(name: "Avenir", size: 12)!
return dataSet
}
}
class CustomChartFormatter: NSObject, IAxisValueFormatter {
var days: [String]
init(days: [String]) {
self.days = days
}
public func stringForValue(_ value: Double, axis: AxisBase?) -> String {
return days[Int(value-1)]
}
}
Swift 5, iOS 13
Using addArc to draw a semi-circle and then using rotation3DEffect to rotate it. On the y axis it works perfectly, but in the x axis is seems broken. Am I doing something wrong here.
I got this code to work. But it is a kludge.
I want to us the x axis on the MyArc2 and not have to use an offset+rotationEffect. The first image is what you see with this code as is.
However if I do use x axis it doesn't draw it correctly.
import SwiftUI
struct ContentView: View {
#State var turn:Double = 0
var body: some View {
return VStack {
Image(systemName: "circle")
.foregroundColor(Color.blue)
.onTapGesture {
withAnimation(.linear(duration: 36)) {
self.turn = 360
}
}
Globe2View(turn: $turn)
} // VStack
}
}
struct Globe2View: View {
struct MyArc : Shape {
func path(in rect: CGRect) -> Path {
var p = Path()
p.addArc(center: CGPoint(x: UIScreen.screenWidth/2, y:UIScreen.screenHeight/2), radius: 64, startAngle: .degrees(90), endAngle: .degrees(270), clockwise: true)
return p
}
}
struct MyArc2 : Shape {
func path(in rect: CGRect) -> Path {
var p = Path()
p.addArc(center: CGPoint(x: UIScreen.screenWidth/2, y:UIScreen.screenHeight/2), radius: 64, startAngle: .degrees(90), endAngle: .degrees(270), clockwise: true)
return p
}
}
#Binding var turn:Double
var body: some View {
ZStack {
ForEach(0..<12) { loop in
MyArc()
.stroke(Color.blue, lineWidth: 2)
.rotation3DEffect(.degrees(self.turn + Double((loop * 30))), axis: (x: 0, y: -1, z: 0), anchor: UnitPoint.center, anchorZ: 0, perspective: 0)
}
ForEach(0..<12) { loop in
MyArc2()
.stroke(Color.green, lineWidth: 2)
.rotation3DEffect(.degrees(self.turn + Double((loop * 30))), axis: (x: 0, y: -1, z: 0), anchor: UnitPoint.center, anchorZ: 0, perspective: 0)
.rotationEffect(.degrees(90))
.offset(x: 20, y: 20)
}
}
}
}
If I change the last few lines so they look like this...
struct MyArc2 : Shape {
func path(in rect: CGRect) -> Path {
var p = Path()
p.addArc(center: CGPoint(x: UIScreen.screenWidth/2, y:UIScreen.screenHeight/2), radius: 64, startAngle: .degrees(0), endAngle: .degrees(180), clockwise: true)
return p
}
}
ForEach(0..<12) { loop in
MyArc2()
.stroke(Color.green, lineWidth: 2)
.rotation3DEffect(.degrees(self.turn + Double((loop * 30))), axis: (x: 1, y: 0, z: 0), anchor: UnitPoint.center, anchorZ: 0, perspective: 0)
// .rotationEffect(.degrees(90))
// .offset(x: 20, y: 20)
}
It is not clear why it is needed MyArc2 as it is the same, but the following, with simple fix in shape, just works as expected (to my mind).
Tested with Xcode 11.4 / iOS 13.4
struct Globe2View: View {
struct MyArc : Shape {
func path(in rect: CGRect) -> Path {
var p = Path()
// used provided dynamic rect instead of hardcoded NSScreen
p.addArc(center: CGPoint(x: rect.width/2, y:rect.height/2), radius: 64, startAngle: .degrees(90), endAngle: .degrees(270), clockwise: true)
return p
}
}
#Binding var turn:Double
var body: some View {
ZStack {
ForEach(0..<12) { loop in
MyArc()
.stroke(Color.blue, lineWidth: 2)
.rotation3DEffect(.degrees(self.turn + Double((loop * 30))), axis: (x: 0, y: -1, z: 0), anchor: UnitPoint.center, anchorZ: 0, perspective: 0)
}
ForEach(0..<12) { loop in
MyArc()
.stroke(Color.green, lineWidth: 2)
.rotation3DEffect(.degrees(self.turn + Double((loop * 30))), axis: (x: 0, y: -1, z: 0), anchor: UnitPoint.center, anchorZ: 0, perspective: 0)
.rotationEffect(.degrees(90))
}
}
}
}
I wanted to extract each of the sprites in the following sprite sheet.
Provided with the key-value of sprite origin points in the sheet.
each sprite into its own SKSpriteNode object
I think through using SKTexture somehow
{
CACTUS_LARGE: { x: 652, y: 2 },
CACTUS_SMALL: { x: 446, y: 2 },
CLOUD: { x: 166, y: 2 },
HORIZON: { x: 2, y: 104 },
MOON: { x: 954, y: 2 },
PTERODACTYL: { x: 260, y: 2 },
RESTART: { x: 2, y: 2 },
TEXT_SPRITE: { x: 1294, y: 2 },
TREX: { x: 1678, y: 2 },
STAR: { x: 1276, y: 2 }
}
$(function () {
var gaugeOptions = {
chart: {
type: 'solidgauge'
},
title: null,
pane: {
center: ['50%', '85%'],
size: '140%',
startAngle: -90,
endAngle: 90,
background: {
backgroundColor: (Highcharts.theme && Highcharts.theme.background2) || '#EEE',
innerRadius: '60%',
outerRadius: '100%',
shape: 'arc'
}
},
tooltip: {
enabled: false
},
// the value axis
yAxis: {
lineWidth: 0,
minorTickInterval: null,
tickPixelInterval: 400,
tickWidth: 0,
title: {
y: -70
},
labels: {
y: 16
}
},
plotOptions: {
solidgauge: {
dataLabels: {
y: 5,
borderWidth: 0,
useHTML: true
}
}
}
};
// The speed gauge
$('#container-speed').highcharts(Highcharts.merge(gaugeOptions, {
yAxis: {
min: 0,
max: 100,
title: {
text: 'Speed'
}
},
series: [{
name: 'Speed',
data: [75]
}]
}));
});
Using the Highchart's solidgauge. Is there a way to have a gradient color up to the value showing on the gauge?
For example if my gauge is from 0 to 100, and at a specific time the value we are showing is 75, I want to show it as a gradient color transitioning from a green left section to small red right section. Of course the remaining 25% of the gauge in this example should not have any color just showing the background color of the gauge.
Is there a way to do that?