I want to generate in google line graph the same in the picture but I cannot to do it. I have A,B and C each of these letters have some values ( A1, A2, B1, B2 ... e.g ) The date is the same it differs only time ( minutes or seconds but day is similar ) I could generate only one point for one letter in one date:
[cols] => Array
(
[0] => Array
(
[id] =>
[label] => Timestamp
[type] => string
)
[1] => Array
(
[id] =>
[label] => A
[type] => number
)
[2] => Array
(
[id] =>
[label] => B
[type] => number
)
[3] => Array
(
[id] =>
[label] => C
[type] => number
)
)
[rows] => Array
(
[0] => Array
(
[c] => Array
(
[0] => Array
(
[v] => 2014-01-30
)
[1] => Array
(
[v] => 120
)
[2] => Array
(
[v] => 100
)
[3] => Array
(
[v] => 35
)
)
)
[1] => Array
(
[c] => Array
(
[0] => Array
(
[v] => 2014-01-30
)
[1] => Array
(
[v] => 334
)
[2] => Array
(
[v] => 55
)
[3] => Array
(
[v] => 15
)
)
)
)
These code gives me 3 seperate lines with only 3 values in one date ( 2014-01-30 ) and next date is also the same ( 2014-01-30 ) But I want to collect all these data in one line as I mentioned in photo below. Thanks in advance for everybody!
Making this work is going to require a bit of trickery. You need to organize your data like this:
Date | Type | Value | Label
---------------------------------
30.01.2014 | A | 75 | A1
30.01.2014 | A | 100 | A2
30.01.2014 | A | 125 | A3
30.01.2014 | B | 150 | B1
30.01.2014 | B | 175 | B2
30.01.2014 | B | 200 | B3
30.01.2014 | C | 180 | C1
30.01.2014 | C | 210 | C2
30.01.2014 | C | 270 | C3
31.01.2014 | A | 75 | A1
31.01.2014 | A | 100 | A2
31.01.2014 | A | 125 | A3
31.01.2014 | B | 150 | B1
31.01.2014 | B | 175 | B2
31.01.2014 | B | 200 | B3
31.01.2014 | C | 180 | C1
31.01.2014 | C | 210 | C2
31.01.2014 | C | 270 | C3
The data needs to be ordered in the order you want the line drawn (so if you want the line to be in the order A1, A2, A3, B1, B2, B3 on 30.01.2014, then that is the order it must be in the table).
Next, you need to use a DataView to split this into multiple series of data to get the points color-coded like your legend:
var view = new google.visualization.DataView(data);
view.setColumns([0, {
type: 'number',
label: 'A',
calc: function (dt, row) {
return (dt.getValue(row, 1) == 'A') ? dt.getValue(row, 2) : null;
}
}, {
type: 'string',
role: 'annotation',
calc: function (dt, row) {
return (dt.getValue(row, 1) == 'A') ? dt.getValue(row, 3) : null;
}
}, {
type: 'number',
label: 'A',
calc: function (dt, row) {
return (dt.getValue(row, 1) == 'B') ? dt.getValue(row, 2) : null;
}
}, {
type: 'string',
role: 'annotation',
calc: function (dt, row) {
return (dt.getValue(row, 1) == 'B') ? dt.getValue(row, 3) : null;
}
}, {
type: 'number',
label: 'A',
calc: function (dt, row) {
return (dt.getValue(row, 1) == 'C') ? dt.getValue(row, 2) : null;
}
}, {
type: 'string',
role: 'annotation',
calc: function (dt, row) {
return (dt.getValue(row, 1) == 'C') ? dt.getValue(row, 3) : null;
}
}, 2]);
Then, when drawing the chart, set the series option to make the points and line appear correctly:
series: {
0: {
// series A options
pointSize: 3,
lineWidth: 0
// you can set the color here with the "color" option if you want
},
1: {
// series B options
pointSize: 3,
lineWidth: 0
// you can set the color here with the "color" option if you want
},
2: {
// series C options
pointSize: 3,
lineWidth: 0
// you can set the color here with the "color" option if you want
},
3: {
// this series draws the line
pointSize: 0,
lineWidth: 1,
visibleInLegend: false,
enableInteractivity: false
// you can set the color here with the "color" option if you want
}
}
See an example here: http://jsfiddle.net/asgallant/bn9tE/
Related
Let's say I have a table listing employee names, certain performance aspects and scores, something like:
Name | Measure | Score
Alice| A | 10
Alice| B | 5
Alice| C | 7
Bob | A | 8
Bob | B | 5
Bob | C | 4
Carol| A | 6
Carol| B | 8
Carol| C | 7
This data is presented in a simple stacked column chart:
What I would like to do, is to find a way to hide all but one the x-axis legends (Name - Alice, Bob, Carol) depending on which name is selected from the slicer above.
So say Alice is selected, only her name would be displayed, the other columns would still be there but without their corresponding names, something like:
Selecting Bob would hide the other two names etc.
Any help would be appreciated
Here you go.
You need to create 3 additional columns as follows.
My code for these is:
Alice =
SWITCH(TRUE(),
'Table'[Name] = "Alice", "Alice",
'Table'[Name] = "Bob", UNICHAR(8203),
'Table'[Name] = "Carol", REPT( UNICHAR(8203), 2),
BLANK()
)
Bob =
SWITCH(TRUE(),
'Table'[Name] = "Alice", UNICHAR(8203),
'Table'[Name] = "Bob", "Bob",
'Table'[Name] = "Carol", REPT( UNICHAR(8203), 2),
BLANK()
)
Carol =
SWITCH(TRUE(),
'Table'[Name] = "Alice", REPT( UNICHAR(8203), 2),
'Table'[Name] = "Bob", UNICHAR(8203),
'Table'[Name] = "Carol", "Carol",
BLANK()
)
Insert a new field parameter as follows.
Create your chart as follows.
I mean,
how can I translate the query
update myTable
set myField1 = myField1 + 1
where myField2 = 'xyz'
Thank you
There are several ways:
db.MyTable
.Where(x => x.MyField2 == "xyz")
.Set(x => x.MyField1, prev => prev.MyField1 + 1)
.Update();
db.MyTable
.Where(x => x.MyField2 == "xyz")
.Update(prev => new MyTable { MyField1 = prev.MyField1 + 1 });
db.MyTable
.Update(x => x.MyField2 == "xyz",
new MyTable { MyField1 = prev.MyField1 + 1 });
in this list, I want to calculate the average between F & M and save its new list.
grade = [{'id':1, 'M':20 , "F":15},
{'id':2, 'M':14 , "F":12},
{'id':3, 'M':17 , "F":18},
{'id':4, 'M':13 , "F":17.5}]
the output should be like this
ave_grade=[{"id":1 ,"average":17.5},{"id":2 ,"average":13},....]
how could I write a good function?
const grade = [{
id: 1,
M: 1,
F: 10
}, {
id: 2,
M: 5,
F: 10
}]
const avg = (...args) => args.reduce((acc, cur) => acc + cur, 0) / args.length
const ave_grade = grade.map(item => ({id: item.id, average: avg(item.M, item.F)}))
console.log(ave_grade)
assuming I have a table that has a list inside
+---------+--------------+
| tag | val |
+---------+--------------+
| [a,b,c] | 1 |
| [a,e] | 2 |
| [f,g] | 3 |
| [e,f] | 4 |
+---------+--------------+
can I create a slicer that when selected will still filter the item inside the list of the tag column?
eg. i select on the filter "a" it will show 1 and 2. "e" will filter 2 and 4, "f" will filter 3 etc.
You may create a measure (returning 1/0) where you use PATHCONTAINS function. We need to remove square bracket and replace commas to pipe "|"; This measure you can put to filter pane in table/matrix visualization https://dax.guide/pathcontains/
ForFilter =
var __selectedTag = SELECTEDVALUE(disconnected[tagList])
var __tags = SELECTEDVALUE('Table'[Tag])
var __path = SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(__tags,",","|"),"[",""),"]","")
return
IF(PATHCONTAINS(__path, __selectedTag),1,0)
EDIT:
version for multiple selection
var __string = CONCATENATEX( VALUES(disconnected[tagList]), disconnected[tagList],"|")
var __tags = SELECTEDVALUE('Table'[Tag])
var __path = SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(__tags,",","|"),"[",""),"]","")
VAR Table0 =
SELECTCOLUMNS(
TOPN(1,
SELECTCOLUMNS(
ADDCOLUMNS (
GENERATE (
ROW ( "tag", __path ,"Text",__string ),
VAR TokenCount =
PATHLENGTH ([Text] )
RETURN
GENERATESERIES ( 1, TokenCount )
),
"Word", PATHITEM ([Text], [Value] )
),
"Word",IF(PATHCONTAINS([tag],[Word]),1,0),
"Tag", [tag],
"Values", [Value]
), [Word],DESC, [Values])
,"Bool", [Word])
return
Table0
I have the following function for which I'd like to add two unit tests, to cover the cases when the selector is or not on the page.
async function getPrice(page, url) {
const priceSelector = '#price';
if (await page.$(priceSelector)) {
return page.$eval(priceSelector, elem => elem.innerText);
}
return null;
}
page is defined in another function:
const browser = await puppeteer.launch();
const page = await browser.newPage();
await page.goto(url);
I've tried to mock page so that page.$(priceSelector) returns truthy or falsy but without success. The examples in the doc to mock modules make sense, but as it is, is my code testable? If not, how should it be structured?
There is only one place need to be refactored, you'd better extract the callback function elem => elem.innerText into a new function.
E.g.
index.ts:
export async function getPrice(page, url) {
const priceSelector = '#price';
if (await page.$(priceSelector)) {
return page.$eval(priceSelector, elem => elem.innerText);
}
return null;
}
index.spec.ts:
import { getPrice } from './';
const page = {
$: jest.fn(),
$eval: jest.fn()
};
beforeEach(() => {
jest.resetAllMocks();
});
test('should eval', async () => {
page.$.mockResolvedValueOnce(true);
page.$eval.mockReturnValueOnce('dummy data');
const actualValue = await getPrice(page, 'example.com');
expect(actualValue).toBe('dummy data');
expect(page.$).toBeCalledWith('#price');
expect(page.$eval).toBeCalledWith('#price', expect.any(Function));
});
test('should return null', async () => {
page.$.mockResolvedValueOnce(false);
const actualValue = await getPrice(page, 'example.com');
expect(actualValue).toBeNull();
expect(page.$).toBeCalledWith('#price');
expect(page.$eval).not.toBeCalled();
});
You can test it like this, but the callback function will not be tested and covered.
Unit test result with coverage report:
PASS src/stackoverflow/58651192/index.spec.ts
✓ should eval (6ms)
✓ should return null (2ms)
----------|----------|----------|----------|----------|-------------------|
File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s |
----------|----------|----------|----------|----------|-------------------|
All files | 85.71 | 100 | 50 | 100 | |
index.ts | 85.71 | 100 | 50 | 100 | |
----------|----------|----------|----------|----------|-------------------|
Test Suites: 1 passed, 1 total
Tests: 2 passed, 2 total
Snapshots: 0 total
Time: 4.786s, estimated 7s
If we extract the callback function for $eval like this:
export const evalCallback = elem => elem.innerText;
We can test it easily:
test('evalCallback', () => {
const actualValue = evalCallback({ innerText: 'unit test' });
expect(actualValue).toBe('unit test');
});
Unit test result with 100% coverage:
PASS src/stackoverflow/58651192/index.spec.ts (9.066s)
✓ should eval (10ms)
✓ should return null (1ms)
✓ evalCallback (1ms)
----------|----------|----------|----------|----------|-------------------|
File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s |
----------|----------|----------|----------|----------|-------------------|
All files | 100 | 100 | 100 | 100 | |
index.ts | 100 | 100 | 100 | 100 | |
----------|----------|----------|----------|----------|-------------------|
Test Suites: 1 passed, 1 total
Tests: 3 passed, 3 total
Snapshots: 0 total
Time: 10.804s