Related
I am trying to create a chart in ng2-charts that will have multiple labels, so for example the x access would have categories of all the Months, above that it will be broken down into each week, and then the bar sections would be something like rainy days, dry days, and snowy days.
I have found their documentation here https://www.chartjs.org/docs/latest/samples/bar/stacked-groups.html and have their example working, but need to expand on that to add another level, is that possible?
The following is what I was able to get figured out. This code will allow create a chart with 3 categories on the x axis divided into 4 categories each. This isn't the full code set, but should hopefully give others an idea on how to proceed if they are stuck.
<canvas baseChart
*ngIf="!loading"
[datasets]="myChartData.chartData"
[options]="barChartOptions"
[legend]="barChartLegend"
[chartType]="barChartType"
>
</canvas>
The following is the code that is used to set the bar chart options from data being returned by the api
this.barChartOptions = {
responsive: true,
scales: {
xAxes: [
{
id: 'xAxis1',
type: 'category',
labels: this.myChartData.xAxis1Labels,
},
{
id: 'xAxis2',
type: 'category',
labels: this.myChartData.xAxis2Labels,
offset: true
},
],
yAxes: [
{
ticks: {
beginAtZero: true,
callback: function(value: number) {if (value % 1 === 0) {return value;}}
}
}
]
},
title: {
display: true,
text: this.myChartData.title,
},
legend: {
labels: {
filter: function (item, chart) {
return item.text !== undefined && item.text !== null;
},
},
},
tooltips: { enabled: false },
hover: { mode: null },
};
This is the model that the api is returning:
import { ChartData } from "chart.js";
export class myChartData {
chartData: Array<ChartData>;
rankXAxisLabels: Array<string>;
namcXAxisLabels: Array<string>;
title: string;
}
Lastly, this is the format of the chartData Array that is being built by the api:
[
{
"data": [1,0,9,0,4,0,5,8,0,0,0,0],
"label": "firstPartOfBar",
"stack": "dataStack",
"backgroundColor": "red",
"hoverBackgroundColor": "red",
"xAxisID": "xAxis1"
},
{
"data": [1,0,8,0,5,0,0,0,0,0,6,7],
"label": "Second Part of Bar",
"stack": "dataStack",
"backgroundColor": "blue",
"hoverBackgroundColor": "blue",
"xAxisID": "xAxis1"
},
{
"data": [4,0,2,0,1,0,0,0,12,14,16,0],
"label": "Third Part of stack",
"stack": "dataStack",
"backgroundColor": "green",
"hoverBackgroundColor": "green",
"xAxisID": "xAxis1"
}
]
I try to display a line chart with two yAxis in Chart.js.
My Code:
window.onload = function() {
var ctx = document.getElementById("chart").getContext("2d");
var myChart = new Chart(ctx, {
type: "line",
data: {
"labels": [
"01.12.2015",
"02.12.2015",
"03.12.2015",
"04.12.2015",
"30.12.2015"
],
"datasets": [{
"label": "DEA Burrweiler Druck Abgabe",
"fill": "false",
yAxisID: "y-axis-0",
"data": [
8.7913,
8.6985,
8.7914,
8.7948,
8.7882
]
}, {
"label": "DEA Burrweiler Druck Zulauf",
"fill": "false",
yAxisID: "y-axis-0",
"data": [
4.5997,
4.5526,
4.5915,
4.5937,
4.5795
]
}, {
"label": "DMS Flemlingen Durchfluss",
"fill": "false",
yAxisID: "y-axis-1",
"data": [
1.9588,
2.4226,
2.1392,
2.223,
1.9048
]
}]
},
options: {
yAxes: [{
position: "left",
"id": "y-axis-0"
}, {
position: "right",
"id": "y-axis-1"
}]
}
});
};
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.1.0/Chart.min.js"></script>
<canvas id="chart" width="400" height="400"></canvas>
The error says: "TypeError: yScale is undefined"
When I remove the yAxisID options in the datasets, the Chart gets displayed but only with one yAxis.
window.onload = function() {
var ctx = document.getElementById("chart").getContext("2d");
var myChart = new Chart(ctx, {
type: "line",
data: {
"labels": [
"01.12.2015",
"02.12.2015",
"03.12.2015",
"04.12.2015",
"30.12.2015"
],
"datasets": [{
"label": "DEA Burrweiler Druck Abgabe",
"fill": "false",
"data": [
8.7913,
8.6985,
8.7914,
8.7948,
8.7882
]
}, {
"label": "DEA Burrweiler Druck Zulauf",
"fill": "false",
"data": [
4.5997,
4.5526,
4.5915,
4.5937,
4.5795
]
}, {
"label": "DMS Flemlingen Durchfluss",
"fill": "false",
"data": [
1.9588,
2.4226,
2.1392,
2.223,
1.9048
]
}]
},
options: {
yAxes: [{
position: "left",
"id": "y-axis-0"
}, {
position: "right",
"id": "y-axis-1"
}]
}
});
};
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.1.0/Chart.min.js"></script>
<canvas id="chart" width="400" height="400"></canvas>
I have the yAxis defined in the chart options. So whats the Problem, what am I missing?
The yAxes should be under a property scales under options, like so
...
options: {
scales: {
yAxes: [{
..
Fiddle - http://jsfiddle.net/dahd27d7/
In my project I store data in active record model and index html document in elasticsearch using mapper-attachments plugin. My document mapping look like this:
include Elasticsearch::Model
settings index: { number_of_shards: 5 } do
mappings do
indexes :alerted
indexes :title, analyzer: 'english', index_options: 'offsets'
indexes :summary, analyzer: 'english', index_options: 'offsets'
indexes :content, type: 'attachment', fields: {
author: { index: "no"},
date: { index: "no"},
content: { store: "yes",
type: "string",
term_vector: "with_positions_offsets"
}
}
end
end
I run a query to double check my doc mapping and the result:
"mappings": {
"feed_entry": {
"properties": {
"content": {
"type": "attachment",
"path": "full",
"fields": {
"content": {
"type": "string",
"store": true,
"term_vector": "with_positions_offsets"
},
It works great (the type: 'attachment' above). I can do the search through html doc perfectly.
I have a performance problem with activerecord which is mysql and I don't really need to store it in database so I decide to migrate to store in elasticsearch.
I am doing an experiment with elasticsearch-persistence gem.
I configure the mapping as below:
include Elasticsearch::Persistence::Model
attribute :alert_id, Integer
attribute :title, String, mapping: { analyzer: 'english' }
attribute :url, String, mapping: { analyzer: 'english' }
attribute :summary, String, mapping: { analyzer: 'english' }
attribute :alerted, Boolean, default: false, mapping: { analyzer: 'english' }
attribute :fingerprint, String, mapping: { analyzer: 'english' }
attribute :feed_id, Integer
attribute :keywords
attribute :content, nil, mapping: { type: 'attachment', fields: {
author: { index: "no"},
date: { index: "no"},
content: { store: "yes",
type: "string",
term_vector: "with_positions_offsets"
}
}
but when i do a query to mapping i got something like this:
"mappings": {
"entry": {
"properties": {
"content": {
"properties": {
"_content": {
"type": "string"
},
"_content_type": {
"type": "string"
},
"_detect_language": {
"type": "boolean"
},
which is wrong. can anyone tell me how to do a mapping with attachment type ?
Really appreciate your help.
In the mean time, I have to hard-code it this way:
def self.recreate_index!
mappings = {}
mappings[FeedEntry::ELASTIC_TYPE_NAME]= {
"properties": {
"alerted": {
"type": "boolean"
},
"title": {
#for exact match
"index": "not_analyzed",
"type": "string"
},
"url": {
"index": "not_analyzed",
"type": "string"
},
"summary": {
"analyzer": "english",
"index_options": "offsets",
"type": "string"
},
"content": {
"type": "attachment",
"fields": {
"author": {
"index": "no"
},
"date": {
"index": "no"
},
"content": {
"store": "yes",
"type": "string",
"term_vector": "with_positions_offsets"
}
}
}
}
}
options = {
index: FeedEntry::ELASTIC_INDEX_NAME,
}
self.gateway.client.indices.delete(options) rescue nil
self.gateway.client.indices.create(options.merge( body: { mappings: mappings}))
end
And then override the to_hash method
def to_hash(options={})
hash = self.as_json
map_attachment(hash) if !self.alerted
hash
end
# encode the content to Base64 formatj
def map_attachment(hash)
hash["content"] = {
"_detect_language": false,
"_language": "en",
"_indexed_chars": -1 ,
"_content_type": "text/html",
"_content": Base64.encode64(self.content)
}
hash
end
Then I have to call
FeedEntry.recreate_index!
before hand to create the mapping for elastic search. Becareful when you update the document you might end up with double base64 encoding of the content field. In my scenario, I checked the alerted field.
At http://jsonapi.org/format/#fetching-includes
Articles belongs to an author. Articles have many comments. Comments belongs to a user.
Trying to understand how to include nested relationships. Take for example: https://www.foo.com/articles?include=comments
You would expect:
{
data: [
{
id: 1,
type: "articles",
attributes: { ... },
relationships: {
author: { ... },
comments: [{ ... }, { ... }],
},
...
},
{ ... }
]
included: [
{
author: { ... },
comment: { ... },
comment: { ... }
{
]
}
Now lets say, you wanted to include the users who wrote those comments. https://www.foo.com/articles?include=comments.user
Should the response look like:
{
data: [
{
id: 1,
type: "articles",
attributes: { ... },
relationships: {
author: { ... },
comments: [{ ... }, { ... }]
},
...
},
{ ... }
]
included: [
{
author: { ... },
comment: { ... },
comment: { ... },
user: { ... },
user: { ... }
{
]
}
Should users (users who wrote the comments) also be in the relationship node, or just in the included node?
If in the relationship node. Should user be nested inside data.relationships.comments? How would that look?
According to the Top Level documentation, the included field should be an array of Resource Objects. This means your included field should look like this:
"included": [
{
"type": "author",
"id": ...,
"attributes": { ... },
"relationships": { ... }
}, {
"type": "comment",
"id": ...,
"attributes": { ... },
"relationships": { ... }
}
]
Also according to the Relationships documentation, it should (when side-loading data) contain a Resource Linkage data field which will contain either a single Resource Identifier in the case of a belongsTo relationship, or an array of resource identifier objects in the case of a hasMany relationship.
belongsTo resource identifier:
"relationships": {
"author": {
"links": { ... },
"data": { "type": ..., "id": ... }
},
"comments": {
"links": { ... },
"data": [
{ "type": ..., "id": ... },
{ "type": ..., "id": ... }
]
}
}
Hi I am stuck on a problem in mongo. Let me first show you an example of the data. This is a record from my table.
{
"_id": "3691149613248",
"following": [{
"content_id": "2584833593728",
"date": "2015-08-20 12:46:55"
}, {
"content_id": "3693447751360",
"date": "2015-09-11 12:17:55"
}, {
"content_id": "2582396936896",
"date": "2015-09-12 07:04:02"
}, {
"content_id": "3697346507456",
"date": "2015-09-14 09:56:03"
}, {
"content_id": "3697755500800",
"date": "2015-09-16 10:05:51"
}, {
"content_id": "2589701320192",
"date": "2015-09-16 10:51:19"
}, {
"content_id": "2585723555136",
"date": "2015-09-16 11:40:26"
}, {
"content_id": "3695996668352",
"date": "2015-09-16 12:50:25"
}, {
"content_id": "3694290368512",
"date": "2015-09-16 12:50:33"
}, {
"content_id": "3691210127552",
"date": "2015-09-16 13:02:57"
}, {
"content_id": "3694134958464",
"date": "2015-09-16 13:06:17"
}, {
"content_id": "3697315148736",
"date": "2015-09-17 06:58:35"
}, {
"content_id": "3692104837824",
"date": "2015-09-17 12:19:12"
}, {
"content_id": "3693400309376",
"date": "2015-09-22 05:43:04"
}]
}
I want to fetch following array with condition that only specific records to fetch i.e. content_ids with prefix 369 and fetch number of content_id specified in limit and offset.
I am using $slice for fetching records for given limit & offset for following array. But how to filter content_id along with $slice.
My current query:
db.collectionName.find({
_id: "3691149613248"
}, {
"following": {
"$slice": [0, 10]
}
});
This is fetching following array with content_id that is specified in limit & offset. But it is fetching all content_id including prefix 258& 369 but I only need content_id with prefix 369 using mongo query.
Can any one help??
You can use combination of $unwind and $match with mongo aggregation to get expected output like:
db.collection.aggregate({
$match: {
"_id": "3691149613248" // you can skip this condition if not required
}
}, {
$unwind: "$following"
}, {
$match: {
"following.content_id": {
$regex: /^369/
}
}
}, {
$group: {
_id: "$_id",
"following": {
$push: "$following"
}
}
})
If you want to apply skip and limit to above query then you can easily use it like:
db.collection.aggregate({
$match: {
"_id": "3691149613248" //use this if you want to filter out by _id
}
}, {
$unwind: "$following"
}, {
$match: {
"following.content_id": {
$regex: /^369/
}
}
}, {
$skip: 4 // you can set offset here
}, {
$limit: 3 // you can set limit here
}, {
$group: {
_id: "$_id",
"following": {
$push: "$following"
}
}
})
EDIT :
If you are using php version less than 5.4 then query will be as:
$search = "369";
$criteria = array(array("$match" => array("_id" => "3691149613248")),
array("$unwind" => "$following"),
array("$match" => array("following.content_id" => array("$regex" => new MongoRegex("/^$search/")))),
array("$skip" => 4), array("$limit" => 3),
array("$group" => array("_id" => "$_id", "following" => array("$push" => "$following"))));
$collection - > aggregate($criteria);
If you are using PHP version greater than 5.3 then just replace { and } braces with [ and ] respectively.