Searchkick (Elasticsearch) histogram with aggregations - extending bounds - ruby-on-rails

I'm using the searchkick gem in a Rails app to access Elasticsearch data. I use the aggregations feature to display a histogram on price; the relevant part of the body_options looks like this:
price: {
histogram: {
field: :low_rate,
interval: 50
}
}
The issue I'm having is that I also need to be able to provide a filter on low_rate. So, for example, if I initially have a histogram that has a range of rates between $50 and $500, and I add to the next query where low_rate < 300, the histogram buckets are recalculated and the chart is entirely redrawn. But in my app I need the chart to still show the same buckets.
This is a pretty common behavior on filters -- see the example below from Airbnb's site, which shows the chart after the upper limit being dragged down -- and so I'm hoping someone might be able to provide some advice about how to achieve this using searchkick.

I can help you with E.S Query if it helps.
{
"size": 0,
"aggs": {
"hist": {
"histogram": {
"field": "low_rate",
"interval": 50,
"extended_bounds": { // <======= It is used to extend bounds
"min": 50, // <========== Specify the min bound
"max": 500 // <========== Specify the max bound
},
"missing":10 // <========== value of the missing docs in the range
}
}
}
}

Related

Configuring gridlines for an AXNumericDataAxisDescriptor when implementing an AXChartDescriptor

I was trying out the Audio Graph APIs. I'm trying to add gridlines to my chart's data axes descriptor, but doing so doesn't seem to make any difference to me. So I'm surely missing something...
I was watching the WWDC21 session "Bring accessibility to charts in your app", and the presenter says: "When you provide grid lines, they're represented in the audio graph as haptic feedback during playback and interaction." And that is quite interesting!
This is how I'm trying to do it.
var accessibilityChartDescriptor: AXChartDescriptor? {
get {
let xAxis = AXNumericDataAxisDescriptor(title: "Lines of code", range: 0...10, gridlinePositions: []) { number in
"\(number) lines"
}
let yAxis = AXNumericDataAxisDescriptor(title: "Lines of code", range: 0...40, gridlinePositions: []) { number in
"\(number) cups"
}
let dataSeriesDescriptor = AXDataSeriesDescriptor(name: "Lines of code per coffees taken", isContinuous: false, dataPoints: [20, 30, 35, 32, 28, 9, 4, 3, 15, 1].enumerated().map({ (point, index) in
AXDataPoint(x: Double(point), y: Double(index), additionalValues: [], label: nil)
}))
return AXChartDescriptor(title: "Cups of coffee vs. lines of code", summary: "This chart shows how coffee consumption impacts coding ability", xAxis: xAxis, yAxis: yAxis, additionalAxes: [], series: [dataSeriesDescriptor])
}
set {}
}
But for me, it already showed grid lines even if I didn't configure them. Once I do, they don't change visually and it is not adding any haptic feedback... So both configuring gridlines or not, the Chart Detail screen looks like this:
Any pointers on how I can achieve having gridlines that provide haptic feedback would be hugely appreciated! Many thanks!

How can I change specific text color in Google Spreadsheets?

First of all, Sorry for my English. English is not my first language.
I simply want to change specific text color in Google Spreadsheets.
Example
"▶Mike: Hey, How is going on?"
In this sentence, I want to change only "▶Mike:" this part.
I have no idea what's going on in Java and programming language.
But I really need this. please help.
Thank you
If you want to change the color of a cell in Google Sheets, you can use one of the two options:
1. Use Sheets API
For updating the color of the text, you will have to use the spreadsheets.batchUpdate method.
Request
POST https://sheets.googleapis.com/v4/spreadsheets/{spreadsheetId}:batchUpdate
Body
{
"requests": [{
"repeatCell": {
"cell": {
"userEnteredFormat": {
"textFormat": {
"bold": true,
"italic": true,
"foregroundColor": {
"blue": 1.0,
"green": 0.0,
"red": 0.0
}
}
}
},
"range": {
"sheetId": 0,
"startRowIndex": 0,
"endRowIndex": 1
},
"fields": "userEnteredFormat(textFormat)"
}
}]
}
If you want to use Java, you might benefit from taking a look at this snippet from here and adapt it according to your task.
However, this option does not offer you the possibility of changing only a part of the text from the cell. For this, you should try using Apps Script.
2. Use Apps Script
Since you want to change a particular number of characters from the cell, you can use the below script
function changeColor() {
var sheet = SpreadsheetApp.getActiveSheet();
var range = sheet.getRange("A1:A2");
var redColor = SpreadsheetApp.newTextStyle()
.setForegroundColor("#ff0000")
.build();
var purpleColor = SpreadsheetApp.newTextStyle()
.setForegroundColor("#871f78")
.build();
var richTextA1 = SpreadsheetApp.newRichTextValue()
.setText("▶Mike: Hey, How is going on?")
.setTextStyle(0, 6, redColor)
.setTextStyle(7, 28, purpleColor)
.build();
var richTextA2 = SpreadsheetApp.newRichTextValue()
.setText("red words, purple words")
.setTextStyle(0, 10, redColor)
.setTextStyle(11, 23, purpleColor)
.build();
range.setRichTextValues([
[richTextA1],
[richTextA2]
]);
}
The above script creates two text styles and later applies them depending on the startOffset and the endOffset parameters which essentially represent which characters will have one of the two text styles.
After executing the above script, this is how the cells will look like:
Reference
Sheets API V4 Method: spreadsheets.batchUpdate;
Apps Script Range Class - setRichTextValues(values).

Grafana/Graphite change y-axis labels to % for line chart

It doesn’t seem like graphite supports changing the y-axis labels to percentages on line charts, but they support it on pie charts, oddly enough: https://graphite.readthedocs.io/en/latest/render_api.html?highlight=y-axis#valuelabels
I looked at the meta data from an svg formatted image. Surely enough they have a y.labels array for y-axis, but you can’t modify it through their Render URL API as a parameter??
"y": {
"labelValues": [0, 0.25, 0.5, 0.75, 1.0],
"labels": ["0 ", "0.25 ", "0.50 ", "0.75 ", "1.00 "],
"step": 0.25
}
Any thoughts? Here’s their documentation on all the y-axis parameters: https://graphite.readthedocs.io/en/latest/render_api.html?highlight=y-axis#yaxisside
Here's what I currently have:
/render?target=asPercent(node.load_avg.one,1)&format=png&yMax=100&yStep=25
This takes the node load average and return percentages. For example, if a value was 0.75 it would convert it to 75. However, the y-axis does not display "%" character next to the ySteps. A small thing, but it would be nice if I could make it say 75% instead of 75...
Here's what I want:
"y": {
"labelValues": [0, 25, 50, 75, 100],
"labels": ["0%", "25%", "50%", "75%", "100%"],
"step": 25
}
Seems you are right. I expect this is easy to implement. I don't have time at the moment but if you want to give it a try I can review your PR and/or give some hints.

Highchart : How to set different step on each xaxis label?

I have sample of array data below :
[
[10,10.23],
[23,58.55],
...,
[-30,170.66],
...,
[-13,3433.36],
...,
[4,47449.77],
...
]
When I generate them to graph, the xaxis label was set to :
0, 5k, 10k, 15k, 20k, 25k, 30k, etc
I would like to set step for xaxis label like :
0, 10, 100, 1000, 10000, 100000
or in logic code equal to :
Math.pow(10,step+1)
I read the Highchart API, never found the solution for this step method. Is there a trick to do that?
Thank you,
Mahdi
I think you are looking for
xAxis: {
type: 'logarithmic',
labels: { //for if you want to hide the 'k' abbreviation for thousands
'{value}'
}
},
See the HighCharts demo on logarithmic charts for more details.

Highcharts: Ensure y-Axis 0 value is at chart bottom

I'm outputting a series of highcharts on a page. In some instances, all data for the specified time period may come back with 0 values.
In such a case, the chart looks like this: http://jsfiddle.net/charliegriefer/KM2Jx/1/
There is only 1 y-Axis label, 0, and it's in the middle of the chart.
I'd like to force that 0 value for the y-Axis to be at the bottom of the chart, which would be consistent with other charts on the page that do have data.
Have tried various yAxis properties, such as "min: 0", "minPadding: 0", "maxPadding: 0", "startOnTick: true".
Seems it should be pretty straightforward, but I'm at a loss :(
Relevant yAxis code:
yAxis: {
min: 0,
minPadding: 0,
startOnTick: true,
title: {
text: ""
}
},
I've found another (and very simple) solution:
You can set y-axis minRange property:
yAxis: {
...
minRange : 0.1
}
This makes Highcharts render yAxis min and max values when all data is 0.
UPDATE:
Highcharts 4.1.9 fix also needs:
plotOptions: {
line: {
softThreshold: false
}
}
updated js fiddle
Only way I could get it to show it "correctly" was by also providing an yAxis max value. I think this is a HighCharts issue when you provide no data elements with a y-value. It draws the best chart it thinks it can.
I had also posted this to the HighCharts forum, where I got a response that seems to work. Unfortunately, it will involve me doing some pre-processing of the data prior to rendering the charts in order to check for all "0" data values... but it'll work. Just wish there was something more "built-in" to HighCharts.
So after summing up the "y" values, if I get 0, the y-axis properties should look like this:
yAxis: {
minPadding: 0,
maxPadding: 0,
min: 0,
max:1,
showLastLabel:false,
tickInterval:1,
title: {
text: ""
}
}
See: http://jsfiddle.net/KM2Jx/2/
The keys seem to be the "max" and "showLastLabel" properties.
Here is a fix I came up with that doesn't require any preprocessing of the data and just let highcharts do it, like it used to before the update. http://jsfiddle.net/QEK3x/
if(chart.yAxis[0].dataMax === 0)
{
chart.yAxis[0].setExtremes(0, 5);
}
You only need to check the first series axis, as if the first one is not 0 then the problem won't occur. I might submit a pull request for this to high-charts, though I kinda feel like they intended for this functionality for some reason.
Since Highcharts 5.0.1, you can also use softMax.
softMax: Number
A soft maximum for the axis. If the series data maximum is greater than this, the axis will stay at this maximum, but if the series data maximum is higher, the axis will flex to show all data.
yAxis: {
min: 0,
softMax: 100,
...
}
Here is an example with a column chart : http://jsfiddle.net/84LLsepj/
Note that currently, in Highcharts 5.0.7, it doesn't seem to be working for all types of charts. As you can see in this other jsfiddle, it doesn't work with line chart.
Just add "min" option
yAxis: {
min: 0
}
For Highcharts 5.x.x, you should be able to just specify:
yAxis: {
softMin: 0,
softMax: 1,
};
This won't work for line chart, which requires that you also set minRange in the same config object as above. Experiment with different values, since defining for example minRange: 6, created a range on yAxis between 0 and 4.
minRange : 1 or whatever you feel so to be displayed in fractions of
softMax : 100 or whatever you feel so
yAxis: {
minRange : 1,
softMax: 100
}

Resources