Categories
Vue 3 Projects

Create a Temperature Converter with Vue 3 and JavaScript

Vue 3 is the latest version of the easy to use Vue JavaScript framework that lets us create front end apps.

In this article, we’ll look at how to create a temperature converter with Vue 3 and JavaScript.

Create the Project

We can create the Vue project with Vue CLI.

To install it, we run:

npm install -g @vue/cli

with NPM or:

yarn global add @vue/cli

with Yarn.

Then we run:

vue create temperature-converter

and select all the default options to create the project.

Create the Temperature Converter

To create the temperature converter, we write:

<template>
  <form @submit.prevent="convert">
    <div>
      <label>temperature in celsius</label>
      <input v-model.number="celsius" />
    </div>
    <button type="submit">convert</button>
  </form>
  <div>{{ celsius }}c is {{ fahrenheit }}f</div>
</template>

<script>
export default {
  name: "App",
  data() {
    return {
      celsius: 0,
      fahrenheit: 0,
    };
  },
  computed: {
    formValid() {
      return !isNaN(+this.celsius);
    },
  },
  methods: {
    convert() {
      if (!this.formValid) {
        return;
      }
      this.fahrenheit = this.celsius * (9 / 5) + 32;
    },
  },
};
</script>

We create the form with an input that takes the temperature in celsius.

We bind the input value to the celsius reactive with v-model .

The number modifier lets us convert it to a number automatically.

In the form , we add the @submit directive to listen for clicks on the button with type submit .

The prevent modifier prevents server-side submissions and lets us do client-side submissions.

Then in the component object, we have the data method that returns the celsius and fahrenheit reactive properties.

We have the formValid computed property that checks if the celsius reactive property is a number.

Then in the convert method,. we check the formValid computed property to see if the form is valid.

Then if it is, we compute the fahrenheit value from the celsius value.

And finally, in the template, we display the results of the convert in the div.

Conclusion

We can create a temperature easily with Vue 3 and JavaScript.

Categories
Vue 3 Projects

Create a Palindrome Checker with Vue 3 and JavaScript

Vue 3 is the latest version of the easy to use Vue JavaScript framework that lets us create front end apps.

In this article, we’ll look at how to create a palindrome checker with Vue 3 and JavaScript.

Create the Project

We can create the Vue project with Vue CLI.

To install it, we run:

npm install -g @vue/cli

with NPM or:

yarn global add @vue/cli

with Yarn.

Then we run:

vue create palindrome-checker

and select all the default options to create the project.

Create the Palindrome Checker

To create the palindrome checker, we write:

<template>
  <form>
    <div>
      <label>word to check</label>
      <input v-model="word" />
    </div>
  </form>
  <div>Is Palindrome:{{ isPalindrome ? "Yes" : "No" }}</div>
</template>

<script>
export default {
  name: "App",
  data() {
    return {
      word: "",
    };
  },
  computed: {
    isPalindrome() {
      const { word } = this;
      return word === word.split("").reverse().join("");
    },
  },
};
</script>

We have the input field in the form.

And we bind the inputted value to the word reactive property with v-model .

Then below that, we have the data method that returns the word reactive property.

Then we add the isPalindrome computed property to check if this.word is a palindrome.

We check if it’s a palindrome by checking if the reversed string is the same as the original.

We call split to split it into an array of characters.

Then we call reverse to reverse the array.

And then we call join to join the characters back into a string.

Then we display that value in the template.

Conclusion

We can create a palindrome checker easily with Vue 3 and JavaScript.

Categories
JavaScript

Add Charts to Our JavaScript App with Anychart — Step Area Charts, Step Line Charts, Stick Charts, and Sunburst Charts

Anychart is an easy to use library that lets us add chart into our JavaScript web app.

In this article, we’ll look at how to create basic charts with Anychart.

Step Area Chart

We can create a step area chart with Anychart.

To add one, we write the following HTML:

<script src="https://cdn.anychart.com/releases/8.9.0/js/anychart-base.min.js" type="text/javascript"></script>

<div id="container" style="width: 500px; height: 400px;"></div>

Then we write the following JavaScript code:

const data = [{
    x: "1995",
    value: 0.10
  },
  {
    x: "1996",
    value: 0.10
  },
  {
    x: "1997",
    value: 0.12
  },
  {
    x: "1998",
    value: 0.13
  },
  {
    x: "1999",
    value: 0.15
  },
];

const chart = anychart.area();
const series = chart.stepArea(data);
series.stepDirection("forward");
chart.container("container");
chart.draw();

We add the Anychart base package with the script tag.

Then we add a div which is used as the container for the chart.

Next, we add the data array with the x and value properties in each entry.

x has the x-axis value.

value has y-axis value.

anychart.area creates the area chart.

chart.stepArea creates the step area chart with our data.

series.stepDirection sets the direction of the steps.

chart.container sets the ID for the container element for the chart.

chart.draw draws the chart.

Step Line Chart

We can create step line charts in a similar way.

For instance, we can write:

const data = [{
    x: "1995",
    value: 0.10
  },
  {
    x: "1996",
    value: 0.10
  },
  {
    x: "1997",
    value: 0.12
  },
  {
    x: "1998",
    value: 0.13
  },
  {
    x: "1999",
    value: 0.15
  },
];

const chart = anychart.stepLine();
const series = chart.stepLine(data);
series.stepDirection("forward");
chart.container("container");
chart.draw();

The data is the same as the previous example.

anychart.stepLine creates the step line chart.

chart.stepLine sets the data.

The rest of the code is the same as the step area chart example.

Stick Chart

We can create a stick easily with Anychart.

To add it, we write:

const data = [{
    x: "1995",
    value: 0.10
  },
  {
    x: "1996",
    value: 0.10
  },
  {
    x: "1997",
    value: 0.12
  },
  {
    x: "1998",
    value: 0.13
  },
  {
    x: "1999",
    value: 0.15
  },
];

const chart = anychart.stick();
const series = chart.stick(data);
chart.container("container");
chart.draw();

We call anychart.stick to create the stick chart.

chart.stick creates the stick chart with our data.

The rest of the code is the same as the other examples.

Sunburst Chart

We can create a sunburst chart with Anychart.

To do this, we write:

<script src="https://cdn.anychart.com/releases/8.9.0/js/anychart-base.min.js" type="text/javascript"></script>
<script src="https://cdn.anychart.com/releases/8.9.0/js/anychart-sunburst.min.js"></script>

<div id="container" style="width: 500px; height: 400px;"></div>

to add the Anychart base and sunburst chart packages and the container div.

Then we write:

const data = [{
  name: "Company A",
  children: [{
      name: "Technical",
      children: [{
          name: "Architects"
        },
        {
          name: "Developers"
        },
      ]
    },
    {
      name: "Sales",
      children: [{
          name: "Analysts"
        },
        {
          name: "Executives"
        }
      ]
    },
    {
      name: "HR"
    },
  ]
}];

const chart = anychart.sunburst(data, "as-tree");
chart.container("container");
chart.draw();

to add the data and the chart.

data has the name and children properties to add the name of the segment and the children segment contents respectively.

Then we call anychart.sunburst to create the sunburst chart with the data .

'as-tree' lets us display the data in the chart as a tree structure.

The rest of the code is the same as the previous examples.

Conclusion

We can add step area charts, step line charts, stick charts, and sunburst charts easily with Anycharts.

Categories
JavaScript

Add Charts to Our JavaScript App with Anychart — Sparklines, Spline Charts, and Spline Area Charts

Anychart is an easy to use library that lets us add chart into our JavaScript web app.

In this article, we’ll look at how to create basic charts with Anychart.

Sparkline

We can create a sparkling easily with Anycharts.

To add one, we write the following HTML:

<script src="https://cdn.anychart.com/releases/8.9.0/js/anychart-base.min.js" type="text/javascript"></script>
<script src="https://cdn.anychart.com/releases/8.9.0/js/anychart-sparkline.min.js"></script>

<div id="container" style="width: 500px; height: 400px;"></div>

Then we add the following JavaScript code:

const stage = anychart.graphics.create('container');
const chart = anychart.sparkline();
chart.container(stage);
chart.data([1.1371, 1.1341, 1.1132, 1.1381, 1.1371]);
chart.bounds(0, 0, 90, 20);
chart.draw();

We add the script tags for the Anychart base package the sparkline package.

Then we add the div which will be used as the container element for rendering the sparkline in.

Next, we create the stage object with the anychart.graphics.create method.

We pass in the ID for the container element to render the sparkline in.

Then we call anychart.sparkline to create the sparkline.

And we add that to the stage by calling chart.container .

chart.data sets the data for the chart.

chart.bounds sets the top left and bottom right corners of the sparkline respectively.

The numbers are in pixels.

chart.draw draws the sparkline.

Spline Area Chart

We can add a spline area chart with Anychart.

For instance, we can write the following HTML:

<script src="https://cdn.anychart.com/releases/8.9.0/js/anychart-base.min.js" type="text/javascript"></script>

<div id="container" style="width: 500px; height: 400px;"></div>

And the following JavaScript code:

const data = [{
    x: "January",
    value: 43
  },
  {
    x: "February",
    value: 46
  },
  {
    x: "March",
    value: 12
  },
  {
    x: "April",
    value: 65
  },
  {
    x: "May",
    value: 71
  }
];

const chart = anychart.area();
const series = chart.splineArea(data);
chart.container("container");
chart.draw();

We only need the Anychart base package to create the spline area chart.

The container div is the same as the previous example.

Next, we create the data array with the data for our chart.

x has the x-axis values.

value has the y-axis values.

anychart.area creates the area chart.

chart.splineArea adds the spline area chart with the data.

The rest of the code is the same as the previous example.

Spline Chart

We can create a spline chart with the anychart.line method.

For instance, we can write:

const data = [{
    x: "January",
    value: 43
  },
  {
    x: "February",
    value: 46
  },
  {
    x: "March",
    value: 12
  },
  {
    x: "April",
    value: 65
  },
  {
    x: "May",
    value: 71
  }
];

const chart = anychart.line();
const series = chart.spline(data);
chart.container("container");
chart.draw();

We call anychart.line to create the line chart.

Then we create the spline chart with chart.spline .

The rest of the code is the same as the other examples.

Conclusion

We can create sparklines, spline charts, and spline area charts with Anychart.

Categories
JavaScript

Add Charts to Our JavaScript App with Anychart — Scatter Line Charts, Scatter Marker Charts, and Sankey Diagrams

Anychart is an easy to use library that lets us add chart into our JavaScript web app.

In this article, we’ll look at how to create basic charts with Anychart.

Scatter Line Chart

We can add a scatter line chart with Anychart.

For instance, we can add the following HTML:

<script src="https://cdn.anychart.com/releases/8.9.0/js/anychart-base.min.js" type="text/javascript"></script>

<div id="container" style="width: 500px; height: 400px;"></div>

Then we can add the following JavaScript code:

const data = [{
    x: 1.3,
    value: 39,
  },
  {
    x: 2.5,
    value: 42,
  },
  {
    x: 3.2,
    value: 54,
  },
  {
    x: 3.9,
    value: 38,
  },
  {
    x: 5.1,
    value: 58,
  }
];

const chart = anychart.scatter();
const series = chart.line(data);
chart.container("container");
chart.draw();

We add the script tag to add the Anychart base package.

The div is the container to render the chart in.

Then we add the data array to add the data into our chart.

Each entry has the x property with the x-axis value.

The value property has the y-axis value.

Then we call anychart.scatter to create the scatter chart.

chart.line lets us add a line to the scatter chart.

We also set the data to render with the method.

chart.container sets the ID of the container to render the chart in.

chart.draw draws the chart.

Scatter Marker Chart

We can create a scatter marker chart with the following code:

const data = [{
    x: 1.3,
    value: 39,
  },
  {
    x: 2.5,
    value: 42,
  },
  {
    x: 3.2,
    value: 54,
  },
  {
    x: 3.9,
    value: 38,
  },
  {
    x: 5.1,
    value: 58,
  }
];

const chart = anychart.scatter();
const series = chart.marker(data);
chart.container("container");
chart.draw();

We have the data array as in the previous example.

And we call anychart.scatter again to create a scatter chart.

chart.marker lets us add the markers to the chart.

We set the data to render with this method.

The rest of the code is the same as the previous example.

Sankey Diagram

We can add a Sankey diagram with Anychart.

To add the chart, we write the following HTML:

<script src="https://cdn.anychart.com/releases/8.9.0/js/anychart-base.min.js" type="text/javascript"></script>
<script src="https://cdn.anychart.com/releases/8.9.0/js/anychart-sankey.min.js"></script>

<div id="container" style="width: 500px; height: 400px;"></div>

Then we add the following JavaScript code:

const data = [{
    from: "Canada",
    to: "France",
    weight: 434
  },
  {
    from: "Canada",
    to: "Germany",
    weight: 343
  },
  {
    from: "Canada",
    to: "Italy",
    weight: 434
  },
  {
    from: "Canada",
    to: "Spain",
    weight: 213
  },
  {
    from: "USA",
    to: "France",
    weight: 341
  },
  {
    from: "USA",
    to: "Germany",
    weight: 343
  },
  {
    from: "USA",
    to: "Spain",
    weight: 232
  }
];

const chart = anychart.sankey(data);
chart.nodeWidth("30%");
chart.container("container");
chart.draw();

We add the Anychart base package and the Sankey package with the script tags.

The div is the same as the previous examples.

Then we add the data array with the from , to and weight properties in each entry.

from and to to has the values which line goes from and to respectively.

weight is the weight of the line.

Next, we call anychart.sankey to create the sankey chart.

chart.nodeWidth has the width of the nodes.

And the rest of the code is the same as the previous examples.

Conclusion

We can add scatter line charts, scatter marker charts, and Sankey diagrams easily with Anychart.