Query Array value of a record - ruby-on-rails-4

I have a record with one of the values stored as an array like this;
#<Health id: 12, district: 43, county: 89, sub_county: 480, name_of_institution: "May Medical Center", money_received: #<BigDecimal:b6ca9318,'0.6E6',9(18)>, date_received: "2013-10-23", use_of_money: "Money used o construct operation theater.", grade_of_health_center: 4, opening_time: "2000-01-01 07:00:00", closing_time: "2000-01-01 18:30:00", **service_offered: ["2", "3", "4", ""]**, other_service_offered: "HIV treatement", male_patients: 54, female_patients: 78, brick_and_wattle: 9, mad_and_wattle: 2, other_structures: 9, source_of_power: [""], other_source_of_power: "Generator", toilet_facilities: true, alternative_disposal: "", separate_toilets: true, running_water: true, alternative_water: "", state_of_water: "Functional", duration_non_functional: "", placenta_pit: true, placental_disposal: "", waste_pit: true, waste_disposal: "", storage_expired_drugs: false, expired_drugs_storage: "They are burnt to ash", pregnant_mother: 15, number_of_beds: 5, delivery_beds: 1, ambulance: true, status_of_ambulance: "Functional", keep_records: true, number_of_staff: 14, medical_staff: 6, resident_medical_staff: 3, created_at: "2014-10-23 17:00:48", updated_at: "2014-10-23 21:23:36">
How can i check the value service_offered: ["2", "3", "4", ""] for existence of '2'
I have tried this;
Health.where("grade_of_health_center = '2'").includes(service_offered: "4").count
I want to retrieve the records having service_offered array containing value 2

Related

Apexcharts DataLabels

Can anyone help in displaying data labels on apex charts I want to show data value instead of %value
I want to show the value 55 or 41 or something likewise on the data label how to do the formatting for this in data labels block
dataLabels: {
enabled: true,
textAnchor: "start",
style: {
colors: ["#fff"]
},
formatter: function(val, opt) {
return opt.w.globals.labels[opt.dataPointIndex] + ": " + val;
},
offsetX: 0,
dropShadow: {
enabled: true
}
},
var options = {
series: [{
name: 'Marine Sprite',
data: [44, 55, 41, 37, 22, 43, 21]
}, {
name: 'Striking Calf',
data: [53, 32, 33, 52, 13, 43, 32]
}],
};
[![Apex Charts datalabel ][1]][1]
It is similar to how you have raised it in the dataLabels formatter, val contains the value of data.
I leave you below a possible dataLabels and an example in codesandbox
dataLabels: {
enabled: true,
textAnchor: "start",
style: { colors: ["#fff"] },
formatter: function (val, opt) {
console.log("VAL: ", val);
let valueFormatted = val.toFixed(0);
console.log("Val formatted: ", valueFormatted);
// return opt.w.globals.labels[opt.dataPointIndex] + ": " + val;
return valueFormatted;
},
offsetX: 0,
dropShadow: { enabled: true }
}
Result of the solution in photo with first solution with val
You can also invent another value within series and put it in the formatter, in the code is the example with valueInvented, and in the formatter function we access it with opt.w.config.series[opt.seriesIndex].valueInvented
const series = [
{
name: "Marine Sprite",
data: [20, 60, 20, 20, 20, 20, 20],
valueInvented: 1
},
{
name: "Striking Calf",
data: [10, 10, 20, 20, 20, 20, 20],
valueInvented: 2
},
{
name: "Tank Picture",
data: [10, 5, 20, 20, 20, 20, 20],
valueInvented: 3
},
{
name: "Bucket Slope",
data: [30, 5, 20, 20, 20, 20, 20],
valueInvented: 4
},
{
name: "Reborn Kid",
data: [30, 20, 20, 20, 20, 20, 25],
valueInvented: 5
}
];
formatter: function (val, opt) {
console.log("VAL: ", val);
console.log("OPT:", opt);
console.log(
"OPT valueInvented: ",
opt.w.config.series[opt.seriesIndex].valueInvented
);
let valueFormatted = val.toFixed(0);
console.log("Val formatted: ", valueFormatted);
return opt.w.config.series[opt.seriesIndex].valueInvented
},
Just add a custom formatter as below:
dataLabels: {
formater(val, opt) {
return opt.w.globals.series[opt.seriesIndex][opt.dataPointIndex]
}
}

Data is not validated in Django Rest Framework

I am trying to pass the data to a serializer like the following:
myDict = {
"invoice_date": "2021-02-24T11:44:13+05:30",
"invoice_number": "12",
"vendor": "4",
"amount": "12",
"gst": "12",
"total_amount": "14",
"transaction_type": "Allot",
"status": "Hold",
"transactions": [
{
"t_no": 47,
"f_mile": "45",
"long_haul": "45",
"l_mile": "45",
"labour": "45",
"others": "54",
"a_t_no": 47,
},
{
"t_no": 102,
"f_mile": "12",
"long_haul": "12",
"l_mile": "21",
"labour": "21",
"others": "21",
"a_t_no": 102,
},
],
"owner": 2,
}
But when I check the validated data in the serialzer it shows it without the transactions data:
{'invoice_date': datetime.datetime(2021, 2, 24, 6, 14, 13, tzinfo=<UTC>), 'invoice_number': '12', 'amount': 12, 'gst': 12, 'total_amount': 14, 'status': 'Hold', 'transaction_type': 'Allot', 'vendor': <Vendor: Vendor object (4)>, 'owner': <User: yantraksh>}
so I tried to check the initial data that is being passed to the serializer :
<QueryDict: {
"invoice_date": ["2021-02-24T11:44:13+05:30"],
"invoice_number": ["12"],
"vendor": ["4"],
"amount": ["12"],
"gst": ["12"],
"total_amount": ["14"],
"transaction_type": ["Allot"],
"status": ["Hold"],
"transactions": [
'[{"t_no":47,"f_mile":"45","long_haul":"45","l_mile":"45","labour":"45","others":"54","a_t_no":47},{"t_no":102,"f_mile":"12","long_haul":"12","l_mile":"21","labour":"21","others": "21","a_t_no":102}]'
],
"owner": [2],
}>
It shows that the transaction data is being passed as a string, what should I change it to in order to get it as validated data ?
Based on the "evidence" in that QueryDict (namely strings wrapped in lists) it sounds like you're not submitting the data as JSON.
You can't use regular HTML form data to submit structured data, you'll need to post JSON data.

how to count number of occurrences of element in list of maps in dart?

I have a list of maps like this
data = [
{
'id': 100,
'quantity': 20
},
{
'id': 101,
'quantity': 25
},
{
'id': 101,
'quantity': 30
}
{
'id': 105,
'quantity': 50
}
]
I am able to count occurrences of element in a list like this
data.forEach((x) => map[x] = !map.containsValue(x) ? (1) : (map[x] + 1));
print(map);
Output :
{{id: 100, quantity: 20}: 1, {id: 101, quantity: 25}: 1, {id: 101, quantity: 30}: 1, {id: 105, quantity: 50}: 1}
But I want to count how many times id = 101 comes in the list so How can I achieve that?
You can use where to filter your array of maps and then use the getter length :
var data = [
{'id': 100, 'quantity': 20},
{'id': 101, 'quantity': 25},
{'id': 101, 'quantity': 30},
{'id': 105, 'quantity': 50}
];
print(data.where((e) => e['id'] == 101).length); // 2

How do I make my Type:Time to work with my Line Chart?

I am working on creating a Line graph that plots points at different times during one day for different days and in order to do that, I need to use the type: time on the xAxes. I have the moment.js downloaded, but the type:time is giving me an error. Without the type:time, the X axis label is too long and makes the graph look bad. Any ideas what I would need to do to fix this?
I have tried to put the data points this way:
data: [ {x: newDate(2019, 06, 24, 08, 00), y: 24}]
but it still didn't work with the type:time
<canvas id="timechart" width="800" height="400" role="img"></canvas>
<script>
var ctx = document.getElementById('timechart');
var timeFormat = 'MM/DD/YY HH:mm';
function newDate(days) {
return moment().add(days).toDate();
}
function newDateString(days) {
return moment().add(days).format();
}
var timechart = new Chart(ctx, {
type: "line",
data: {
labels: [newDate(0), newDate(1), newDate(2), newDate(3),
newDate(4), newDate(5), newDate(6), newDate(7), newDate(8), newDate(9),
newDate(10), newDate(11),
newDate(12), newDate(13), newDate(14), newDate(15),
newDate(16), newDate(17)],
datasets: [
{
data: [12, 21, 32, 25, 16, 14, 7, 25, 18, 20, 22, 15,
17, 11, 19, 28, 30, 10],
label: 'D Speed',
fill: false,
backgroundColor: 'rgba(102, 147, 188, 1)',
borderColor: 'rgba(102, 147, 188, 1)',
radius: 4,
hoverRadius: 5,
}, {
data: [8, 16, 24, 30, 20, 10, 12, 21, 32, 15, 17, 11,
22, 18, 29, 17, 8],
label: 'U Speed',
fill: false,
backgroundColor: 'rgba(255, 147, 44, 1)',
borderColor: 'rgba(255, 147, 44, 1)',
radius: 4,
hoverRadius: 5,
}
],
},
options: {
title: {
display: true,
},
scales: {
xAxes: [{
display: true,
type: 'time',
}],
yAxis: [{
display: true,
}]
},
}
});
</script>
I expect the x axis labels to show just the MM/DD/YY HH:MM but it is actually not showing the chart at all.
The code you shared worked for me. Make sure that you've included Moment and the non-bundled version of Chart.js in your project. E.g.:
<script src="https://cdn.jsdelivr.net/npm/moment#2.24.0"></script>
<script src="https://cdn.jsdelivr.net/npm/chart.js#2.8.0"></script>
Also, check your browser's developer console to see if there are any JavaScript errors that can help give more clues as to the problem. You can access in Chrome by hitting F12.

How to return two lists to chartjs from Django View

I am using chartjs to render a barchart. For this I need to pass two lists in the format like [1, 2, 3] & [3, 2, 1]. I am making an AJAX call to Django which returns the two lists (I have not added the code to get the data from the database yet). The graph works fine for one list but not sure how to pass the second list.
I tried to pass the two lists as json and tried to use each of the lists in the success function of the ajax call but the graph does not render properly. With one list the graph is working fine
below is the code for the ChartJs AJAX call
$.ajax({
async: pasys,
type: "GET",
url: purl,
data: pdata,
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function(ldata) {
var barData = {
labels: ["Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct",
"Nov", "Dec", "Jan", "Feb", "Mar"],
datasets: [
{
label: "DL1",
backgroundColor: 'rgba(220, 220, 220, 0.5)',
pointBorderColor: "#fff",
data: ldata.data1
},
{
label: "Non-DL1",
backgroundColor: 'rgba(100, 200, 300, 0.5)',
pointBorderColor: "#aaa",
data: ldata.data2
}
]
};
var barOptions = {
responsive: true
};
var ctx2 =
document.getElementById("opendemandtrend").getContext("2d");
new Chart(ctx2, {type: 'bar', data: barData, options:barOptions});
* below is the code for the django view *
def gldh_productivitymetric_opendemandtrend_get(request):
lcompanyid = request.GET.get("pcompanyid")
lpmid = request.GET.get("ppmid")
data = json.dumps({"data1": "[12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1]",
"data2": "[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]"})
return HttpResponse(data, content_type="application/json")
You're passing in strings as the values, instead of lists. Don't do that.
data = json.dumps({"data1": [12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1],
"data2": [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]})