New data to array - vue.js

I would like to have three inputs which add data to each data array.
Input looks like this:
<input type="range" min="0" max="10" step="1" v-model="newData">{{ newData }}
<button #click="addData(newData, 'first')">Add</button>
and script
const app = new Vue({
el: '#app',
data: {
newData: 5,
dimensions: [
{
title: 'first',
data: [0, 2, 5, 9, 5, 10, 3, 5, 0, 0, 1, 8, 2, 9, 0]
},
{
title: 'second',
data: [1, 3, 8, 1, 2, 3, 3, 3, 5, 1, 9, 2, 4, 6, 0]
},
{
title: 'third',
data: [6, 1, 6, 1, 2, 5, 3, 9, 5, 1, 0, 2, 4, 4, 10]
}]
},
methods: {
addData() {
???
}
}
})
What kind of should my addData method be? I can't push to particular dimension. I just manage to add new one.
I'm glad if anyone can help me :)

Change your addData() method to accept two parameters, the data and the dimension to which to push. Then it should look like the following:
addData(mData, mDimension) {
this.dimensions.forEach((dimension) => {
if (dimension['title'] === mDimension) {
dimension['data'].push(parseInt(mData));
}
});
}
Don't forget to pass the data and dimension in your method call.

Related

How to select (Highlight) each mapped item on Click in React Native

I have an Array of Strings that I have mapped using the Map funtion and rendered a Text with each item. Now if I click any mapped Text I want to highlight that clicked Text. Moreover, I want to keep on highlighting the next Text after some delay and so on.
How to do it efficiently? So far I have witten the following code and I am able to Highlight the clicked text with the help of an extra style based on the selected page and index clicked. Now I want to have to select the next mapped item automatically after some delay.
function QiratContainer() {
const [selectedIndex, setSelectedIndex] = useState<any>(2)
const [selectedPage, setSelectedPage] = useState<number>(1)
return (
<SafeAreaView >
<FlatList
data={quranData}
showsVerticalScrollIndicator={false}
renderItem={({ item, index }) => (
<View style={styles.pageContainer}>
<PageHeader surahName={item.firstSurahName} juzzNumber={item.juz} />
<View style={{ flex: 1 }}>
{item.ayahs.map((ayah: any, index: any) => (
<>
{ayah.name !== undefined &&
<SurahHeader ayah={ayah} />
}
<Text onPress={() => playAyah(ayah, index)} style={[styles.ayahStyle, selectedIndex === index && selectedPage === ayah?.page && styles.selectedAyah]}>{ayah?.text?.replace("بِسْمِ اللَّهِ الرَّحْمَٰنِ الرَّحِيمِ", "")}</Text>
</>
))}
</View>
<PageFooter pageNumber={item.page} />
</View>
)}
/>
</SafeAreaView>
)
function playAyah(ayah: any, index: any) {
console.log(ayah.audio)
//Setting the Selected Index and the specific page of that index to highlight the selected ayah...
console.log(index)
// console.log(ayah.page)
setSelectedIndex(index)
setSelectedAyah(ayah)
}
}
const styles = StyleSheet.create({
ayahStyle: {
fontSize: 24,
fontWeight: 'bold',
marginVertical: 8,
color: 'black'
},
selectedAyah: {
backgroundColor: 'grey'
},
pageContainer: {
width: width,
height: height - 20,
},
})
Following is my JSON Array that is used in the FlatList and Map
[
{
"juz": 1,
"page": 1,
"firstSurahName": "سورة الفاتحة",
"ayahs": [
{
"number": 1,
"name": "سورة الفاتحة",
"englishName": "Al-Faatiha",
"englishNameTranslation": "The Opening",
"revelationType": "Meccan",
"numberInPage": 0,
"ayahsNumber": 7
},
{
"number": 1,
"text": "بِسْمِ اللَّهِ الرَّحْمَٰنِ الرَّحِيمِ",
"numberInSurah": 1,
"juz": 1,
"page": 1,
"hizbQuarter": 1,
"ayahsNumber": 7,
"surahNumber": 1,
"numberInPage": 1,
"audio": "https://everyayah.com/data/ahmed_ibn_ali_al_ajamy_128kbps/001001.mp3"
},
{
"number": 2,
"text": "الْحَمْدُ لِلَّهِ رَبِّ الْعَالَمِينَ",
"numberInSurah": 2,
"juz": 1,
"page": 1,
"hizbQuarter": 1,
"ayahsNumber": 7,
"surahNumber": 1,
"numberInPage": 2,
"audio": "https://everyayah.com/data/ahmed_ibn_ali_al_ajamy_128kbps/001002.mp3"
},
{
"number": 3,
"text": "الرَّحْمَٰنِ الرَّحِيمِ",
"numberInSurah": 3,
"juz": 1,
"page": 1,
"hizbQuarter": 1,
"ayahsNumber": 7,
"surahNumber": 1,
"numberInPage": 3,
"audio": "https://everyayah.com/data/ahmed_ibn_ali_al_ajamy_128kbps/001003.mp3"
},
{
"number": 4,
"text": "مَالِكِ يَوْمِ الدِّينِ",
"numberInSurah": 4,
"juz": 1,
"page": 1,
"hizbQuarter": 1,
"ayahsNumber": 7,
"surahNumber": 1,
"numberInPage": 4,
"audio": "https://everyayah.com/data/ahmed_ibn_ali_al_ajamy_128kbps/001004.mp3"
},
{
"number": 5,
"text": "إِيَّاكَ نَعْبُدُ وَإِيَّاكَ نَسْتَعِينُ",
"numberInSurah": 5,
"juz": 1,
"page": 1,
"hizbQuarter": 1,
"ayahsNumber": 7,
"surahNumber": 1,
"numberInPage": 5,
"audio": "https://everyayah.com/data/ahmed_ibn_ali_al_ajamy_128kbps/001005.mp3"
},
{
"number": 6,
"text": "اهْدِنَا الصِّرَاطَ الْمُسْتَقِيمَ",
"numberInSurah": 6,
"juz": 1,
"page": 1,
"hizbQuarter": 1,
"ayahsNumber": 7,
"surahNumber": 1,
"numberInPage": 6,
"audio": "https://everyayah.com/data/ahmed_ibn_ali_al_ajamy_128kbps/001006.mp3"
},
{
"number": 7,
"text": "صِرَاطَ الَّذِينَ أَنْعَمْتَ عَلَيْهِمْ غَيْرِ الْمَغْضُوبِ عَلَيْهِمْ وَلَا الضَّالِّينَ",
"numberInSurah": 7,
"juz": 1,
"page": 1,
"hizbQuarter": 1,
"ayahsNumber": 7,
"surahNumber": 1,
"numberInPage": 7,
"audio": "https://everyayah.com/data/ahmed_ibn_ali_al_ajamy_128kbps/001007.mp3"
}
],
"number": 1,
"counter": 1
}
]
I always do this stuff by creating a state called "selected" and I save the ID of the selected .map child component in it.
In the .map children components I put classname={selected == id ? "cssSelected" : "cssNotSelected"}
And when I click on a .map child component: onClick={() => setSelected(id)}

useGetMany - fetches data by sending traction but does not update data field - code copied and used from react-admin document

I had used the useGetMany to get topics for mathpapers. I can see based on reference the getMany
Tracation goes and fetch data as shown in console.log response. But the data is not stored in the
" const { data, loaded, error } - as it shows array of same size but undefined objects in console.log prints."
Please suggest what I am missing here.
const Mysubtopics = ({ record }: any) => {
console.log(record);
const subtopicsIds = record.subtopic_id;
console.log('subtopicsID', subtopicsIds);
const { data, loaded, error } = useGetMany('subtopics', subtopicsIds);
//if (error) return <Error />;
if (error) return <span>Error</span>;
if (!loaded) return <span>Loading</span>;
if (!data) return null;
console.log('data', data);
const allsubtopics = data.reduce((acc, subtopic) => {
acc = acc.concat(subtopic.name.en);
return acc;
}, '');
return (
<span>
{allsubtopics}
{record.id}
</span>
);
};
const MathPaperList = (props: any) => {
const classes = useStyles();
const isSmall = useMediaQuery((theme: Theme) =>
theme.breakpoints.down('sm')
);
return (
<List
{...props}
filters={<MathPaperFilter />}
sort={{ field: 'generated_at', order: 'DESC' }}
exporter={exporter}
>
{isSmall ? (
<SimpleList
primaryText={record => record.title}
secondaryText={record => <Mysubtopics record={record} />}
tertiaryText={record =>
new Date(record.generated_at).toLocaleDateString()
}
/>
) : ...........some more for desktop error is in above secondaryText.
I can see the following console.log -
getMany subtopics {"ids":[10,12,14,3,5,8,1,2,4]}
index.js:31
Object
data: Array(9)
0: {id: 1, name: {…}}
1: {id: 2, name: {…}}
2: {id: 3, name: {…}}
3: {id: 4, name: {…}}
4: {id: 5, name: {…}}
5: {id: 8, name: {…}}
6: {id: 10, name: {…}}
7: {id: 12, name: {…}}
8: {id: 14, name: {…}}
length: 9
Object
index.js:30 getMany subtopics {"ids":[10,12,14,3,5,8,1,2,4]}
MathPaperList.tsx:137 {id: 3, title: "3rd test for batch 2:30", batch_id: 1, subtopic_id: Array(3), generated_at: Thu Dec 30 2021 05:30:00 GMT+0530 (India Standard Time), …}
MathPaperList.tsx:139 subtopicsID (3) [10, 12, 14]
MathPaperList.tsx:146 data (3) [undefined, undefined, undefined]
MathPaperList.tsx:137 {id: 2, title: "2nd test for batch 2:30", batch_id: 1, subtopic_id: Array(3), generated_at: Sat Dec 26 2020 05:30:00 GMT+0530 (India Standard Time), …}
MathPaperList.tsx:139 subtopicsID (3) [3, 5, 8]
MathPaperList.tsx:146 data (3) [undefined, undefined, undefined]
__proto__: ...
.........
Here is data.tsx
mathpapers: [
{
id: 1,
title: '1st test for batch 2:30',
batch_id: 1,
subtopic_id: [1, 2, 4],
generated_at: new Date('2020-12-06'),
difficulty: 80,
mark1q: 0,
mark2q: 5,
mark3q: 0,
mark4q: 6,
mark5q: 2,
},
{
id: 2,
title: '2nd test for batch 2:30',
batch_id: 1,
subtopic_id: [3, 5, 8],
generated_at: new Date('2020-12-26'),
difficulty: 40,
mark1q: 3,
mark2q: 0,
mark3q: 4,
mark4q: 3,
mark5q: 2,
},
.....
],
subtopics: [
{
id: 1,
name: { en: 'Sport' },
},
{
id: 2,
name: { en: 'Technology' },
},
.....
]
Here the missing part, which is never documented in the react-admin was you need to have resourses in the App.tsx file to hook up the resource to get data, so need below 2 resource lines with subtopics in the App.tsx -
import subtopics from './subtopics';
.....
const App = () => {
return (
<Admin
title=""
dataProvider={dataProvider}
customReducers={{ theme: themeReducer }}
customRoutes={customRoutes}
authProvider={authProvider}
dashboard={Dashboard}
loginPage={Login}
layout={Layout}
i18nProvider={i18nProvider}
disableTelemetry
>
<Resource name="mathpapers" {...mathpapers} />
<Resource name="subtopics" {...subtopics} />
{/*<Resource
subtopics file is :
export default {
};

Place point over boxplot using plotlyjs and vue

I'm trying to place point over boxplot, however my attempt results in the point placed next to the boxplot:
data: [
{
x: [1, 2, 3, 4, 4, 4, 8, 9, 10],
type: "box",
name: "Set 1"
},
{
x: [5],
name: 'My special marker',
text: 'Some really interesting hover info',
marker: {
size: 5
}
},
],
layout: {
title: 'Revenues',
font:{
size:10
},
margin: {
l: 0,
r: 0,
b: 15,
t: 25,
pad: 0
},
height: 90,
dragmode: "pan"
},
Is it possible to place that point over boxplot using vue-plotly library?
Here I recreted the issue:
https://codesandbox.io/s/vue-plotly-delsw?file=/src/App.vue
EDIT:
For the desired result I need that addiotional point to be on top of the chart like this:
One solution is to set the size of the point's marker and the width of the bar such that they overlap each other. For example:
data: [
{
x: [1, 2, 3, 4, 4, 4, 8, 9, 10],
type: "box",
name: "Set 1",
width: 30,
},
{
x: [5],
name: "My special marker",
text: "Some really interesting hover info",
marker: {
size: 30
}
}
],

Something wrong with end property of React Native Big Calendar

I'm trying to make a calendar of event using react-native-big-calendar
when i believe it's suppose to work with both start and end time, but somehow it's not work when i uncomment the end property
here are the codes:
const events = [
{
title: 'Meeting',
start: new Date(2020, 5, 20, 10, 0),
// end: new Date(2020, 5, 20, 22, 30),
},
{
title: 'Coffee break',
start: new Date(2020, 1, 14, 15, 45),
// end: new Date(2020, 1, 14, 16, 30),
},
{
title: 'Repair my car',
start: new Date(2020, 5, 20, 7, 45),
// end: new Date(2020, 5, 20, 13, 30),
},
]
class BookingListScreen extends Component {
constructor(props) {
super(props);
this.state = {
}
}
render() {
console.log('logged by phuognn start: ', new Date(2020, 5, 20, 10, 0));
console.log('logged by phuognn end: ',new Date(2020, 5, 20, 22, 30));
return (
<View style={{ flex: 1 }}>
<Calendar
height={Dimensions.get('window').height}
style={styles.calendar}
events={events}
height={600}
eventCellStyle={{ backgroundColor: 'blue' }}
/>
</View>
);
}
}
if I uncomment the end property of events, the calendar show no event.
Wonder why?
The end property is required on the type BaseEvent. So you must specify it!
node_modules\react-native-big-calendar\build\interfaces.d.ts
export interface BaseEvent {
start: Date;
end: Date;
title: string;
}
If what you're looking for is an event which lasts all day, use event's time.
const events = [
{
title: 'Meeting',
start: new Date(2020, 1, 11, 0, 0),
end: new Date(2020, 1, 12, 0, 0),
}
]

Highcharts-vue - Using the tooltip / label formatter()

I previously used the label formatter to customise the Y-Axis labels in a chart, but I'm not sure how to do this using highcharts-vue because of scope.
See the following pseudo code;
export default {
data () {
return {
currencySymbol: '$',
title: 'My Chart',
points: [10, 0, 8, 2, 6, 4, 5, 5],
chartType: 'Spline',
chartOptions: {
chart: {
type: 'spline'
},
title: {
text: 'Sin chart'
},
yAxis: {
gridLineDashStyle: 'Dot',
labels: {
style: {
color: '#fff'
},
formatter: function () {
return this.currencySymbol + this.axis.defaultLabelFormatter.call(this)
}
},
series: [{
data: [10, 0, 8, 2, 6, 4, 5, 5],
color: '#6fcd98'
}]
}
}
}
In the full app, the currencySymbol property is updated in the response of an AJAX call.
Is there an elegant way of achieving this?
The most recommended way would be to use Computed Properties, JS Arrow function, and call Highcharts.Axis.prototype.defaultLabelFormatter.
First of all we need to move a whole chart configuration inside of a computed property, e.g called chartOptions. Then we just need to refactor the formatter function a bit, so that it would be arrow function, and create the variable with symbol defined in component data (!important). After that, the this keyword will indicate on the component object, and refer the symbol direcly by calling the variable created on the top of the function. I prepared the example, so please take a lok on it.
Live example: https://codesandbox.io/s/highcharts-vue-demo-icuzw
Code:
data() {
return {
currencySymbol: "$",
title: "My Chart",
points: [10, 0, 8, 2, 6, 4, 5, 5],
chartType: "Spline"
};
},
computed: {
chartOptions() {
var symbol = this.currencySymbol;
return {
chart: {
type: "spline"
},
title: {
text: "Sin chart"
},
yAxis: {
gridLineDashStyle: "Dot",
labels: {
style: {
color: "#000"
},
formatter: label => {
return (
symbol + Highcharts.Axis.prototype.defaultLabelFormatter.call(label)
);
}
}
},
series: [
{
data: [10, 0, 8, 2, 6, 4, 5, 5],
color: "#6fcd98"
}
]
};
}
}
Kind regards!