Google Sheets Chart of one line with two values - charts

My data looks like this:
A B
3 5000
2 6218
4 9435
I need to create a chart where A is the number of points/block size and B is the Value of each point.
For example, the date above will create a line graph where the 2 first points will have the value of 5000, the next 2 points will have the value of 6218, and the next 4 points will have the value of 9435.
The graph I need will look like the automatic one that will be generated for this data:
5000
5000
5000
6218
6218
9435
9435
9435
9435

use the original data table to build a new data table.
for each row in the original table, add a row to the new table however many times 'A' represents.
var x = 0;
for (var row = 0; row < rawData.getNumberOfRows(); row++) {
for (var a = 0; a < rawData.getValue(row, 0); a++) {
chartData.addRow([x, rawData.getValue(row, 1)]);
x++;
}
}
see following working snippet...
google.charts.load('current', {
packages:['corechart']
}).then(function () {
var rawData = google.visualization.arrayToDataTable([
['A', 'B'],
[3, 5000],
[2, 6218],
[4, 9435]
]);
var chartData = new google.visualization.DataTable();
chartData.addColumn('number', 'X');
chartData.addColumn('number', 'Y');
var x = 0;
for (var row = 0; row < rawData.getNumberOfRows(); row++) {
for (var a = 0; a < rawData.getValue(row, 0); a++) {
chartData.addRow([x, rawData.getValue(row, 1)]);
x++;
}
}
var container = document.getElementById('chart_div');
var chart = new google.visualization.LineChart(container);
chart.draw(chartData, {
height: 288,
pointSize: 4,
vAxis: {
minValue: 0
}
});
});
<script src="https://www.gstatic.com/charts/loader.js"></script>
<div id="chart_div"></div>

Related

Why is it the data not updating immediately when I navigate to a different flutter page, but updates on refresh?

Constant.dart
List<String> a= [];
List<String> b= [];
List<String> c= [];
List check = [a, b, c];
List<String> names= ["x", "y", "z"];
getFirebaseData(String date){
_firestore.collection('something').doc('anything').get().then((value) => {
for (int i=0; i < 3; i++)
check[i].clear(),
for (int i = 0; i < 3; i++)
for (int j=0; j < 3; j++)
{check[i].add(value.data()['${names[i]}'][date]['$j'])},
});
}
Page 1
void _onSelectionChanged(DateRangePickerSelectionChangedArgs args) async{
final splitted = args.value.toString().split(' ');
String date = splitted[0].toString();
await getFirebaseData(date);
Navigator.pushNamed(context, '/page2');
}
Page 2
print("a" +a.toString());
print("b" +b.toString());
print("c" +c.toString());
Here, I am expecting the print blocks to be the values x, y and z. However I am not getting that when I am navigated to that page (Page 2). I am only getting data when I refresh the page (Page 2).
Why is this so? What must be done?

Why does my buffer geometry fail when I try to load in vertices, faces and normal from .mat file?

I want to load in my matlab geometry into my three.js scene. My 3D data is saved in a struct .mat file which contains .vertices, .faces, .VertexNormals and .VertexColorData arrays. I am able to load it into JavaScript and use buffer geometry and set attributes to store the data into a mesh geometry.
var keyName = keysArray[0];
meshGeometry = new THREE.BufferGeometry();
var index = 0;
var positions = new Float32Array(bfjson.data[keyName].vertices.length * 3);
for (let i = 0; i < bfjson.data[keyName].vertices.length; i++) {
positions[index++] = bfjson.data[keyName].vertices[i][0];
positions[index++] = bfjson.data[keyName].vertices[i][1];
positions[index++] = bfjson.data[keyName].vertices[i][2];
}
meshGeometry.setAttribute(
'position',
new THREE.BufferAttribute(positions, 3));
var index = 0;
var vectornormals = new Float32Array(bfjson.data[keyName].VertexNormals.length * 3);
for (let i = 0; i < bfjson.data[keyName].VertexNormals.length; i++) {
vectornormals[index++] = bfjson.data[keyName].VertexNormals[i][0];
vectornormals[index++] = bfjson.data[keyName].VertexNormals[i][1];
vectornormals[index++] = bfjson.data[keyName].VertexNormals[i][2];
}
meshGeometry.setAttribute(
'normal',
new THREE.BufferAttribute(vectornormals, 3));
var index = 0;
//var faces = new Uint16Array(bfjson.data[keyName].faces.length * 3);
var faces = [];
for (let i = 0; i < bfjson.data[keyName].faces.length; i++) {
faces[index++] = bfjson.data[keyName].faces[i][0];
faces[index++] = bfjson.data[keyName].faces[i][1];
faces[index++] = bfjson.data[keyName].faces[i][2];
}
meshGeometry.setIndex(faces);
// default color attribute
const colors = [];
for (let i = 0, n = meshGeometry.attributes.position.count; i < n; ++i) {
colors.push(1, 1, 1);
}
meshGeometry.setAttribute('color', new THREE.Float32BufferAttribute(colors, 3));
for (let i = 0; i < bfjson.data[keyName].CData.length; i++) {
CData[i] = (bfjson.data[keyName].CData[i]);
}
meshGeometry.setAttribute('perfusion', new THREE.Float32BufferAttribute(CData, 1));
mesh.geometry = meshGeometry;
updateColors();
The vertex coloring works fine. However, I end up with mesh with index faces or normal not connecting into a normal surface.
mesh with index faces or normal not connecting up into a normal surface.
I am not sure what I am doing wrong. I will be extremely grateful for any help provided.
Edit--
I have made a jsfiddle to help.
https://jsfiddle.net/marieO/5zdhsk78/68/
But you need to download the .mat file first then upload it to the scene. (as I was unable to add it to the jsfiddle)
https://dev.kingsvirtualanatomyandhistology.kcl.ac.uk//models/mat/p21_newmod.mat
Thanks for posting a working example with the steps needed to reproduce the error. It makes it much easier to help.
You have 652 vertices in your Matlab geometry. When using .setIndex(), these indices have to be in the [0, 651] range, because JavaScript arrays start at index 0. However, your faces data ranges from [1, 652], which means all your triangles are off by 1 vertex.
This is easily solvable by adding a -1 when assigning the index:
var index = 0;
var faces = [];
for (let i = 1; i < bfjson.data[keyName].faces.length; i++) {
faces[index++] = bfjson.data[keyName].faces[i][0] - 1;
faces[index++] = bfjson.data[keyName].faces[i][1] - 1;
faces[index++] = bfjson.data[keyName].faces[i][2] - 1;
}
meshGeometry.setIndex(faces);
Result:

Removing months from google visualisation line chart

My x-axis for google visualisation chart is javascript new Date(year+i, 0, 0). However when I print the line chart. It shows M J S at the bottom with the year number.
How can I remove these M J S (which I presume are May June and September).
the following snippet re-produces the problem, with year number combined with month abbreviations...
2020 M J S 2021 M J S etc...
google.charts.load('current', {
packages: ['corechart']
}).then(function () {
var data = new google.visualization.DataTable();
data.addColumn('date', 'Date');
data.addColumn('number', 'Mutual Fund');
for (var y = 2020; y < 2024; y++) {
for (var m = 0; m < 12; m++) {
data.addRow([new Date(y, m, 1), (10000 + y + m)]);
}
}
var container = document.getElementById('chart');
var chart = new google.visualization.LineChart(container);
chart.draw(data);
});
<script src="https://www.gstatic.com/charts/loader.js"></script>
<div id="chart"></div>
to only show year, you can use hAxis option format.
hAxis: {
format: 'yyyy'
}
however, this could cause the year to repeat (depending on the width of the chart)...
2020 2020 2020 2020 2021 2021 2021
see following snippet...
google.charts.load('current', {
packages: ['corechart']
}).then(function () {
var data = new google.visualization.DataTable();
data.addColumn('date', 'Date');
data.addColumn('number', 'Mutual Fund');
for (var y = 2020; y < 2024; y++) {
for (var m = 0; m < 12; m++) {
data.addRow([new Date(y, m, 1), (10000 + y + m)]);
}
}
var container = document.getElementById('chart');
var chart = new google.visualization.LineChart(container);
chart.draw(data, {
hAxis: {
format: 'yyyy'
}
});
});
<script src="https://www.gstatic.com/charts/loader.js"></script>
<div id="chart"></div>
to ensure the year doesn't repeat, we must also provide hAxis option ticks.
ticks must be in the same format as the data on the x-axis, in this case a date.
so we provide the jan date for each year...
hAxis: {
format: 'yyyy',
ticks: [new Date(2020, 0, 1), new Date(2021, 0, 1), new Date(2022, 0, 1)]
}
you should be able to create the ticks dynamically, from the data.
see following snippet for an example...
google.charts.load('current', {
packages: ['corechart']
}).then(function () {
var data = new google.visualization.DataTable();
data.addColumn('date', 'Date');
data.addColumn('number', 'Mutual Fund');
var ticks = [];
for (var y = 2020; y < 2024; y++) {
for (var m = 0; m < 12; m++) {
data.addRow([new Date(y, m, 1), (10000 + y + m)]);
if (m === 0) {
ticks.push(new Date(y, m, 1));
}
}
}
var container = document.getElementById('chart');
var chart = new google.visualization.LineChart(container);
chart.draw(data, {
hAxis: {
format: 'yyyy',
ticks: ticks
}
});
});
<script src="https://www.gstatic.com/charts/loader.js"></script>
<div id="chart"></div>

How can I get a reValue in a specific position of the matrix?

I'm new in sprite kit. I'm taking the value at a specific position of a matrix, but I really have a problem when I print that value. The simulator prints: optional(nil value) instead of printing just the value.
So, how can I get the reValue in a specific position of the matrix?
Code:
NumColumns = 4
NumRows = 4
func matrix() {
var valor = "0"
var principal = "0"
for var column = 0; column < NumColumns; column++ {
for var j = 0; j < NumRows; j++ {
valor = "\(numbers[column, j])"
cont++
principal = "\(cont)"
if valor != "0" {
numbers[column, j] = valor + principal
println("\(numbers[column, j])") //This print show: optional(nil >value)
}
else {
numbers[column, j] = principal
}
}
}
}
You can create a two-dimensional matrix of strings with the following:
var numColumns = 4
var numRows = 4
// Create a 4x4 matrix of Strings
var array = [[String]](count: numColumns, repeatedValue:[String](count: numRows, repeatedValue:String()))
// Assign a string to a matrix element
array[1][2] = "element at (1,2)"
println (array[1][2])

How to make double[,] x_List in C#3.0?

I ned to implement the multi-linear regression in C#(3.0) by using the LinESt function of Excel.
Basically I am trying to achieve
=LINEST(ACL_returns!I2:I10,ACL_returns!J2:K10,FALSE,TRUE)
So I have the data as below
double[] x1 = new double[] { 0.0330, -0.6463, 0.1226, -0.3304, 0.4764, -0.4159, 0.4209, -0.4070, -0.2090 };
double[] x2 = new double[] { -0.2718, -0.2240, -0.1275, -0.0810, 0.0349, -0.5067, 0.0094, -0.4404, -0.1212 };
double[] y = new double[] { 0.4807, -3.7070, -4.5582, -11.2126, -0.7733, 3.7269, 2.7672, 8.3333, 4.7023 };
I have to write a function whose signature will be
Compute(double[,] x_List, double[] y_List)
{
LinEst(x_List,y_List, true, true); < - This is the excel function that I will call.
}
My question is how by using double[] x1 and double[] x2 I will make double[,] x_List ?
I am using C#3.0 and framework 3.5.
Thanks in advance
double[,] xValues = new double[x1.Length, x2.Length];
for (int i = 0; i < x1.Length; i++)
{
xValues[i, 0] = x1[i];
xValues[i, 1] = x2[i];
}
Actually it should be
double[,] xValues = new double[x1.Length, x2.Length];
int max = (new int[]{ x1.Length,x2.Length}).Max();
for (int i = 0; i < max; i++)
{
xValues[0, i] = x1.Length > i ? x1[i] : 0;
xValues[1, i] = x2.Length > i ? x2[i] : 0;
}
Samir is right, that i should call Max() once outside the iterator rather than each iteration, i've amended this.
The side of the multi-dimensional array is incorrect in both your answer and bablo's. In addition, the call to Max on every iteration in bablo's answer seems really slow, especially with large numbers of elements.
int max = (new int[] { x1.Length, x2.Length }).Max();
double[,] xValues = new double[2, max];
for (int i = 0; i < max; i++)
{
xValues[0, i] = x1.Length > i ? x1[i] : 0;
xValues[1, i] = x2.Length > i ? x2[i] : 0;
}