Make custom fixed intervals chartjs - vue.js

Need to make fixed interval with number values[0,100000,300000,500000,5000000] for left side of graph as this:
scales: {
y: {
max: 5000000 ,
beginAtZero: true,
grid: {
color: '#FFFFFF05',
},
ticks:{
color: '#FFFFFF20',
font: {
size: 10,
},
count: 5,
}
},
Actual intervals, that depends on count.
One way I found depends on values: https://www.chartjs.org/docs/latest/samples/scales/log.html
I don't need any dependencies, just fixed numbers.

Related

Mouseevent handler is not working in the vue-mapbox package

I am trying to make my map zoom in to a cluster when the user clicks the cluster. The map is built with mapbox gl and Vue-Mapbox. I understand I can use the getClusterExpansionZoom() method to do so, but the first step is detecting which cluster the user clicked on. My #click handler does not detect clicks. Why not? What must I change? Cheers
<template>
<div>
<MglMap>
<MglGeojsonLayer
class="mgl-clusters-layer"
layerId="clustersLayerId"
:layer="clustersLayer"
:source="clustersSource"
sourceId="clustersSourceId"
#click="clickedCluster()"
/>
</div>
</template>
These variations also do not work...
#click="clickedCluster"
#map-click="clickedCluster()"
#click.prevent="clickedCluster"
Here is my event handler...
methods: {
clickedCluster() {
console.log("clicked cluster");
}
}
Here is the definition of the clustersSource object
clustersSource: {
type: "geojson",
cluster: true,
clusterRadius: 25,
clusterProperties: { sum: ["+", ["get", "docCount"]] },
data: {
type: "FeatureCollection",
features: []
}
},
data.features array of simple geojson points
Here is the definition of clustersLayer...
clustersLayer: {
id: util.getRandomValue(),
type: "circle",
filter: ["has", "point_count"],
paint: {
"circle-color": "#6a0dad",
"circle-opacity": 0.4,
"circle-stroke-color": "#6a0dad",
"circle-stroke-width": 1,
"circle-radius": [
"step",
["get", "sum"],
8,
100,
10,
1000,
12,
10000,
14,
100000,
16,
1000000,
18
]
}
},
This works...
//Make map zoom to a districts cluster when user clicks the cluster
this.map.on("click", "clustersLayerId", function(e) {
var features = $this.map.queryRenderedFeatures(e.point, {
layers: ["clustersLayerId"]
});
$this.map.easeTo({
center: features[0].geometry.coordinates,
zoom: $this.map.getZoom() + 1
});
});
You also have to specify the id in the clustersLayer object...
clustersLayer: {
id: "clustersLayerId",
type: "circle",
filter: ["has", "point_count"],
paint: {
"circle-color": "#6a0dad",
"circle-opacity": 0.4,
"circle-stroke-color": "#6a0dad",
"circle-stroke-width": 1,
"circle-radius": [
"step",
["get", "sum"],
8,
100,
10,
1000,
12,
10000,
14,
100000,
16,
1000000,
18
]
}
},

Vue Chart.js - simple dot/line on bar chart

I'd need to add a simple dot/vertical line on my bar chart that has a dynamical X value, and 0 for Y value. Preview of what I need (the red dot):
Where the green values are dynamic.
Preview of my current state:
Where 3.30 should be the X coordinate of the dot - [3.30, 0].
I'm using Vue chart for the charts and I tried do create a mixed one with the bar and scatter but scatter requires type: 'linear' for it's xAxis which doesn't suit my need for the bar chart.
So I tried with chartjs-plugin-annotation and it's box type which accepts "coordinates" but the problem here is that the X value must be a fixed value on the X axis (labels object). If I put for the X axis [3,0] it will work, but if there is a decimal number, like [3.5, 0], it won't work.
// data
options: {
responsive: true,
maintainAspectRatio: false,
legend: {
display: false
},
scales: {
yAxes: [{
ticks: {
min: 0,
max: 1,
stepSize: 0.1
}
}]
}
}
// computed
labels: [1, 2, 3, 4, 5, 6], // fixed value, there are always 6 bars
datasets: [
{
label: 'Values',
backgroundColor: '#f89098',
data: this.tableInputValues // array of decimal values
}
]
So, my question is how to put a "simple" dot, or a vertical line, on a Chart.js bar chart where the dot has a dynamical value for the X axis -> [dynamic value, 0].
FYI - it's about Expected value
As far as I understand Vue Chart works using canvas (as seen on Demo page).
So, my suggestion here is to retrieve the canvas node representing the chart in your DOM and dynamically write the desired dot. For example:
var c = document.getElementById("bar-chart"); //hereby assuming canvas named "bar-chart"
var ctx = c.getContext("2d");
ctx.fillStyle = "#ff0000"; //red color for the dot
ctx.beginPath();
let yPosition = c.height - 5; //fixed y position
let xPosition = 35; //that's the dynamic expected value
ctx.arc(xPosition, yPosition, 2.5, 0, 2 * Math.PI);
ctx.fill();
Here you find a demo showing how to achieve that using Vue. In this scenario, you need to wrap the code to draw a dot on the canvas in a afterDraw hook. This hook needs to be attached to the chart component as a plugin, so like this:
...
mounted () {
//adding the plugin to draw the red dot
this.addPlugin({
id: 'chart-plugin',
afterDraw: function (chart) {
var c = chart.canvas;
var ctx = c.getContext("2d");
ctx.fillStyle = "#ff0000";
ctx.beginPath();
let xPosition = 742; //x positioning to be calculated according to your needs
let yPosition = c.height - 28;
ctx.arc(xPosition, yPosition, 3, 0, 2 * Math.PI);
ctx.fill();
}
});
//actual chart rendering
this.renderChart({
...
});
}
...
For the sake of completeness, here you find the list of all the available hooks of Chart.js plugin API.
This is my solution for your problem https://jsfiddle.net/huynhsamha/e54djwxp/
And this is screenshot for the result
In my solution, I use type="line" and both x-axis and y-axis with type="linear". I also add property options to <chart> for using options in ChartJS
<div id="vue">
<chart type="line" :data="data" :options="options"></chart>
</div>
The options will set-up x-axis and y-axis to render the data points and expected value:
options: {
scales: {
xAxes: [{
type: 'linear',
ticks: {
min: 1,
max: 6,
stepSize: 1
}
}],
yAxes: [{
type: 'linear',
ticks: {
min: 0,
max: 1,
stepSize: 0.1
}
}]
}
}
And the data will have 2 datasets. The first is data points, use type line, and the second is the expected value which use type bubble.
data: {
datasets: [{
label: 'Frequency Data',
data: dataPoints.map(({ val, freq }) => ({
x: val,
y: freq
})),
backgroundColor: 'rgba(72, 202, 59, 0.4)',
borderColor: 'rgba(72, 202, 59, 1)'
}, {
label: 'Expected Value',
type: 'bubble',
data: [{
x: expectedValue,
y: 0,
r: 8 // radius
}],
backgroundColor: 'rgba(255, 68, 0, 0.4)',
borderColor: 'rgba(255, 68, 0, 1)'
}]
},
In datasets, we have dataPoints and expectedValue, it will be fetched from API to get your data points. I also simulate the simple API for data points:
// simulate the API to get data points
const retrieveData = () => [
{ val: 1, freq: 0.15 },
{ val: 2, freq: 0.25 },
{ val: 3, freq: 0.3 },
{ val: 4, freq: 0.2 },
{ val: 5, freq: 0.1 },
{ val: 6, freq: 0.45 }
]
// fetch your data here, return array of JSON { val, freg }
const dataPoints = retrieveData() || [];
// calculate expected value = sum( val * freq ) each i in dataPoints
const expectedValue = dataPoints.reduce((cur, { val, freq }) => cur + val * freq, 0).toFixed(4);
You can run snippet or run on fiddle https://jsfiddle.net/huynhsamha/e54djwxp/92/
<script async src="//jsfiddle.net/huynhsamha/e54djwxp/92/embed/js,html,css,result/dark/"></script>

Limit the input entered in ExtJs textfield/numberfield

I have requirement where the textfield/numberfield present should only allow values between 0 to 99999.99 i.e. 5 digits with 2 decimal places.
The user should be restricted to use any invalid number.
I'm bad at regular expression but will that solve my problem ?
Any pointer to solve this issue will be appreciated.
I need to restrict user to enter any values beyond 5 digit. after 5 digit only a decimal can be placed and beyond that 2 digits
Any suggestions ?
You need to add a custom validation type VTypes.
addCustomVtypes : function() {
var mRegex = /^\d{0,5}(\.\d{0,2})?$/;
Ext.apply(Ext.form.field.VTypes, {
mRegex : function(val, field) {
return ralphaNumHyphen.test(val);
},
mRegexText : 'Invalid input. Message here.',
mRegexMask : /\d{0,5}(\.\d{0,2})?$/i
});
}
You could call this on application launch.
Then, on your textfield, add vtype: mRegex.
Ext.create('Ext.form.Panel', {
title: 'On The Wall',
width: 300,
bodyPadding: 10,
renderTo: Ext.getBody(),
items: [{
xtype: 'numberfield',
anchor: '100%',
name: 'bottles',
fieldLabel: 'Bottles of Beer',
value: 0,
maxValue: 99999.99,
minValue: 0
}],
buttons: [{
text: 'Take one down, pass it around',
handler: function() {
this.up('form').down('[name=bottles]').spinDown();
}
}]
});
from:http://docs-origin.sencha.com/extjs/4.0.7/#!/api/Ext.form.field.Number

sencha touch dynamic chart

in Sencha Touch 2.1 how can I load the chart dynamically from json, also with dynamic fields store, chart axes, and chart series,
I know maybe this is too much, but I need to display many kind of data, If I create 1 chart component for each display means I have to create more than 15 chart component, I'm afraid it get bloated
I did not complete this dynamically, but I made it seem dynamic.
I first request a user to fill out a form.
I also have multiple panels that holds charts with empty stores, in the form of several different layouts.
Based on the user's form, I show and hide panels, or chart when they need to be displayed only after loading the store with the required data.
yes it is bulky, and they are static, but I found it slightly easier to handle than dynamically loading.
EDIT
After thinking,
have you tried a function like
function dynamiccharts(var1, var2, var3){
return Ext.chart.Chart({
....
})
}
variables would include things like data, url, store or etc.
This is my example creating a chart on controller inside a panel: axis, series, store fields, url are became parameters, PAR_FORM is global variable showing the difference between views, I'm using this code for another chart (Column, Pie)
Ext.define("Geis.controller.app", {
extend: "Ext.app.Controller",
config: {
refs: {
mainview: 'mainview',
barchartview: 'barchartview',
btnShowChartAnggaran: '#btnShowChartAnggaran'
},
control: {
'btnShowChartAnggaran': {
tap: 'onShowChartAnggaran'
}
}
}
createBarChart: function(fields, series_xfield, series_yfield, url) {
this.getBarchartview().add(new Ext.chart.CartesianChart({
id: 'barchartgenerateview',
store: {
fields: fields,
proxy: {
type: 'jsonp',
url: url
}
},
background: 'white',
flipXY: true,
colors: Geis.view.ColorPatterns.getBaseColors(),
interactions: [
{
type: 'panzoom',
axes: {
"left": {
allowPan: true,
allowZoom: true
},
"bottom": {
allowPan: true,
allowZoom: true
}
}
},
'itemhighlight'
],
series: [{
type: 'bar',
xField: series_xfield,
yField: series_yfield,
highlightCfg: {
strokeStyle: 'red',
lineWidth: 3
},
style: {
stroke: 'rgb(40,40,40)',
maxBarWidth: 30
}
}],
axes: [{
type: 'numeric',
position: 'bottom',
fields: series_yfield,
grid: {
odd: {
fill: '#e8e8e8'
}
},
label: {
rotate: {
degrees: -30
}
},
maxZoom: 1
},
{
type: 'category',
position: 'left',
fields: series_xfield,
maxZoom: 4
}]
}));
Ext.getCmp('barchartgenerateview').getStore().load();
},
onShowChartAnggaran: function() {
this.getBarchartview().remove(Ext.getCmp('barchartgenerateview'), true);
if (PAR_FORM == 'Dana Anggaran') {
this.createBarChart(['kode', 'keterangan', 'nilai'], 'keterangan', 'nilai',
Geis.util.Config.getBaseUrl() + 'anggaran/laporan/json/get_dana_anggaran_json/');
} else if (PAR_FORM == 'Alokasi Anggaran') {
this.createBarChart(['kode', 'keterangan', 'belanja_pegawai', 'belanja_barang', 'belanja_modal'],
'keterangan', ['belanja_pegawai', 'belanja_barang', 'belanja_modal'],
Geis.util.Config.getBaseUrl() + 'anggaran/laporan/json/get_alokasi_json/');
}
this.getMainview().animateActiveItem(1, {type:'slide', direction:'left'});
}
});
base on my experiment if you want to activate the interactions features you need to set the chart id dynamically too, for example by creating Global Counter Variable

Ext.dataview.ListView : Disable Overscroll?

In the sencha scrollable views, like Ext.dataview.ListView, we can always scroll the list of elements out of the screen.
If I take this example : http://dev.sencha.com/deploy/touch/examples/production/kitchensink/#demo/list
Is there a way to block the list item "Alana Wiersma" ? I want this item to be with "top <= 0px".
Your problem is exactly called overscrolling. Try this:
scrollable : {
direction: 'horizontal',
directionLock: true,
momentumEasing: {
momentum: {
acceleration: 30,
friction: 0.5
},
bounce: {
acceleration: 0.0001,
springTension: 0.9999,
},
minVelocity: 5
},
outOfBoundRestrictFactor: 0
}
My first guess would be to set up the scroller of the list with an initial offset
config:{
scrollable:{
direction:'vertical',
initialOffset : {x: 0, y: VALUE}
}
}
And replace VALUE with whatever value you need
Hope this helps