Tuesday, November 27, 2018

JavaScript Array sort by several parameters

Array.prototype allows us to sort array just by 1 parameter. I have created my own method 'sortBy' to fix this gap. The method also allows to sort by desc. Let's say we have the following array
  1. var objects = [{'Name': 'John', 'Age': 23, 'Profession': 'Accountant'},
  2. {'Name': 'Adam', 'Age': 18, 'Profession': 'Engineer'},
  3. {'Name': 'Vik', 'Age': 46, 'Profession': 'Architect'},
  4. {'Name': 'Jane', 'Age': 36, 'Profession': 'Developer'},
  5. {'Name': 'Rikky', 'Age': 28, 'Profession': 'Developer'}];
var objects = [{'Name': 'John', 'Age': 23, 'Profession': 'Accountant'},
    {'Name': 'Adam', 'Age': 18, 'Profession': 'Engineer'},
    {'Name': 'Vik', 'Age': 46, 'Profession': 'Architect'},
    {'Name': 'Jane', 'Age': 36, 'Profession': 'Developer'},
    {'Name': 'Rikky', 'Age': 28, 'Profession': 'Developer'}];
The following function call sorts the array by 'Profession' then by 'Name' descendantly
  1. sortBy(objects, 'Profession', {'Name': 'desc'});
  2. console.table(objects);
sortBy(objects, 'Profession', {'Name': 'desc'});
console.table(objects);
The result will be the following:

Now let's have a look at the method implementation.

Wednesday, November 14, 2018

Sorting C3 chart tooltip

C3 is very simple free JavaScript library that allows to create various types of chart literally in few lines of code. C3 is based on D3 so we must plugin it as well

  1. <script src="https://cdnjs.cloudflare.com/ajax/libs/d3/4.2.2/d3.min.js"></script>
  2. <script src="https://unpkg.com/c3@0.6.8/c3.js"></script>
  3. <link href="https://unpkg.com/c3@0.6.8/c3.css" rel="stylesheet">
  4. <div id="chart"></div>
  5. <script>
  6. var chart = c3.generate({
  7. bindto: '#chart',
  8. data: {
  9. columns: [
  10. ['data1', 30, 200, 100, 400, 250],
  11. ['data2', 50, 20, 10, 40, 25],
  12. ['data3', 150, 10, 40, 40, 15],
  13. ['data4', 80, 40, 70, 20, 45],
  14. ['data5', 30, 60, 35, 90, 40]
  15. ]}
  16. });
  17. </script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/d3/4.2.2/d3.min.js"></script>
  <script src="https://unpkg.com/c3@0.6.8/c3.js"></script>
  <link href="https://unpkg.com/c3@0.6.8/c3.css" rel="stylesheet">

  <div id="chart"></div>
  <script>
    var chart = c3.generate({
     bindto: '#chart',
     data: {
       columns: [
        ['data1', 30, 200, 100, 400, 250],
        ['data2', 50, 20, 10, 40, 25],
        ['data3', 150, 10, 40, 40, 15],
        ['data4', 80, 40, 70, 20, 45],
        ['data5', 30, 60, 35, 90, 40]
       ]}
      });
  </script>

That small code snippet produces such a beautiful chart with a tooltip:


0123405010015020025030035040001234data1data2data3data4data5


But as you can see, the data in the tooltip are not sorted. The goal of this post is to show how to sort it. 

Thursday, November 8, 2018

Adding legend to D3 chart

D3 is very powerful free JavaScript library that allows to do many things such as DOM manipulations, working with data and shapes, smoth transitions between UI states etc. I show just a simple example how I adedd a legend into a coloured barchart.

4.744.682.331.322
SydneyMelbourneBrisbaneAdelaidePerth

Friday, November 2, 2018

PowerShell Get, Filter, Where, Copy in a single line

The task is the following: create a simple script for gathering textual log files from several different places via network, filter them by name and recent update date, display files information like name, datetime, length and copy them to a specified destination. It is pretty achievable through the PowerShell functionality.