Saturday 31 March 2018

css sticky header


title
title
content
content
content
content
content
content


<style>
.my-title{
    height:30vh;
    width:100%;
    background-color:darkorange;
}

.my-header{
    height:20vh;
    width:100%;
    background-color:aqua;
}

.my-content{
    height:200vh;
    width:100%;
    background-color:lawngreen;
}

.my-sticky{
    top:0;
    position:absolute;
}

.my-sticky+.my-content{
    padding-top:40vh;
}

.my-body{
    height:100vh;
    overflow-y:scroll; 
    text-align: center;
    font-size: 10vh;
}

.my-outer-body{
    position:relative;
}



</style>

<br />
<div class="my-outer-body" id="outer_body">
    <div class="my-body" id="my_body" onscroll="scroll_function()">
        <div class="my-title">
        title<br />
        title</div>
        <div class="my-header" id="sticky_header">
        header</div>
        <div class="my-content" id="my_content">
        content<br />
        content<br />
        content<br />
        content<br />
        content<br />
        content</div>
     </div>
</div>
<script type="text/javascript">
       
        var is_sticky = false;

        function scroll_function () {         

            var sticky_header = document.getElementById("sticky_header");
            var my_content = document.getElementById("my_content");

            var bodyRect = document.getElementById("outer_body").getBoundingClientRect();
            var headerRect = sticky_header.getBoundingClientRect();
            var contentRect = my_content.getBoundingClientRect();

            var header_Offset_body = headerRect.top - bodyRect.top;
            var content_Offset_header = contentRect.top - headerRect.bottom;


            if (!is_sticky && header_Offset_body <= 0) {
                sticky_header.classList.add("my-sticky")
                sticky_header.style.right = "3vh"; 
                sticky_header.setAttribute("style", "width:" + my_content.clientWidth + "px"); 
                is_sticky = true;
            }

            if (is_sticky && content_Offset_header > 0) {
                sticky_header.classList.remove("my-sticky");
                is_sticky = false;
            }

        }     

    </script>

reference:
https://www.w3schools.com/howto/howto_js_sticky_header.asp
https://stackoverflow.com/questions/442404/retrieve-the-position-x-y-of-an-html-element
https://stackoverflow.com/questions/35070308/how-to-set-width-of-div-to-another-divs-width

Friday 30 March 2018

css parallax


back
middle
front


<style>
.layers {
    -webkit-perspective: 1px;
    -webkit-perspective-origin: 50% 50%;
    height: 50vh;
    overflow-y: auto;
    /*margin-top:-20%;*/
}

.layer {
    position: absolute;
    margin: auto;
    width: 100%;
    height: 100%;
    -webkit-transform-style: preserve-3d;
    font-size: 150px;
    text-align: center;
    padding:100% 0;
}

.layer-front { 
    /*background-color: rgba(0, 255, 0, 0.7);*/
    color: red;
}

.layer-middle {
    -webkit-transform: translateZ(-1px) scale(2);
    -moz-transform: translateZ(-1px) scale(2);
    /*background-color: rgba(196, 0, 0, 0.7);*/
    color:green;
}

.layer-back {
    -webkit-transform: translateZ(-2px) scale(3);
    -moz-transform: translateZ(-2px) scale(3);
    /*background-color: rgba(196, 0, 196, 0.7);*/
    color:blue;
}
</style>

<div class="layers">
        <div class="layer layer-front">front</div>
        <div class="layer layer-middle">middle</div>
        <div class="layer layer-back">back</div>
    </div>

----------------------------------------------------------------
The scale factor can be calculated with 1 + (translateZ * -1) / perspective

reference:
https://www.w3schools.com/howto/howto_css_parallax.asp
https://keithclark.co.uk/articles/pure-css-parallax-websites/

Hai Nan trip







view and download pictures:

kijiji sell - digital scale



digital scale, fully working. stainless platform 10cm x 10cm. max weight 3000g, precision 0.1g.
Weighing Units: g, gn, ct oz,,ozt. dwt. Calibration weight is not included.

digital scale, fully working, stainless steel platform 5cm x 5cm. max load 200g, precision 0.01g.
Weighing Units: g, gn, ct oz,,ozt. dwt. Calibration weight is not included.

Monday 19 March 2018

加拿大20大城市的富人得赚这么多

移民到了加拿大,移风易俗之其一是不问别人的钱财和收入,当然别人也不会将他的财产底数说给你听。对于我们生活的这个地方,熟悉的亲朋和近邻过得如何,你可能知道一些,对身边之外再大些范围的只能是雾里看物,凭着感觉去判断了。那么究竟在加拿大你是穷人还是富人,今天小编就带你认识一下在加拿大对于富人的定义。

  富人这个词,有加拿大不同城市是有不同定义的。

  当今,在加拿大富人意味着什么呢?当然,没有一个“富人”的定义,但是自大萧条以来,我们一直听到很多关于“1%”这个名词,我们经济中最高收入者似乎正在积累越来越多的世界财富。

1203130116231117-600x400.jpg

  那么,在加拿大年薪多少才能晋升为1%的富豪阶级呢?根据统计局数据,在2015年,要想进入这个1%顶尖俱乐部,年收入只要达到22.54万加元即可。


  《赫芬顿邮报》要求加拿大统计局按照城市对2016年人口普查的收入进行分析,他们发现城市间存在一些非常显著的差异。例如:在卡尔加里你想成为收入上层1%的富人,你的收入几乎要是魁北克省舍布鲁克(Sherbrooke)或者安大略省南部的尼亚加拉地区的上层1%收入的人的三倍。
  以下是加拿大20个最大城市,要想进入上层1%所需的收入,以及要成为上层收入20%所需的收入,这也是对“高收入”的定义。

  舍布鲁克(Sherbrooke),魁省:172,069元

32_162404159.jpg

  个人收入前20%:51,413元

  家庭年收入中值:73,250元

  圣凯瑟琳斯-尼亚加拉(St Catharines-Niagara),安省:177,591元

niagara-falls-1752042_640-600x400.jpg

  个人收入达前20%:53,393元

  家庭年收入中值:74,140元

  魁北克城(Quebec City):189,121元

frontenac-2257154_640-600x337.jpg

  个人收入达前20%:59,169元

  家庭年收入中值:87,570元

  温莎(Windsor),安大略省:200,561元

32_1A251339.jpg

  个人收入达前20%:57,950元

  家庭年收入中值:78,700元

  伦敦,安大略省:203,743元

32_1A302456.jpg

  个人收入达前20%:58,203元

  家庭年收入中值:80,570元

  温尼伯(Winnipeg):203,935元

32_1A31Kb.jpg

  个人收入达前20%:58,631元

  家庭年收入中值:81,880元

  哈利法克斯(Halifax):204,622元

halifax-2370263_640-600x297.jpg

  个人收入达前20%:61,125元

  家庭年收入中值:85,940元

  奥沙华(Oshawa),安大略省:206,696元

32_1A350431.jpg

  个人收入达前20%:66,207元

  家庭年收入中值:92,080元

  基奇纳-剑桥-滑铁卢(Kitchener-Cambridge-Waterloo)地区,安省。:214,098元

32_1A40C22.jpg

  个人收入达前20%:61,419元

  家庭年收入中值:86,930元

  维多利亚(Victoria),卑诗省:214,373元

victoria-bc-1242840_640-600x399.jpg

  个人收入达前20%:62,940元

  家庭年收入中值:89,640元

  蒙特利尔(Montreal):224,060元

32_1A452260.jpg

  个人收入达前20%:56,348元

  家庭年收入中值:76,950元

  里贾纳(Regina),萨省:232,623元

32_1A5064U.jpg

  个人收入达前20%:71,864元

  家庭年收入中值:97,940元

  汉密尔顿(Hamilton),安省:236,176元

32_1A51J59.jpg

  个人收入达前20%:62,978元

  家庭年收入中值:87,590元

  萨斯卡通(Saskatoon):240,932元

32_1A531424.jpg

  个人收入达前20%:68,683元

  家庭年收入中值:94,580元

  渥太华(安省部分):244,534元

canada-1343649_640-600x399.jpg

  个人收入达前20%:74,488元

  家庭年收入中值:104,070元

  温哥华:246,266元

32_1A60Q44.jpg

  个人收入达前20%:60,147元

  家庭年收入中值:79,930元

  圣约翰(St John’s):256,918元

32_1A622629.jpg

  个人收入达前20%:66,542元

  家庭年收入中值:96,320元

  埃德蒙顿(Edmonton):297,928元

32_1A644302.jpg

  个人收入达前20%:75,773元

  家庭年收入中值:101,870元

  多伦多:301,883元

toronto-1298016_640-600x399.jpg

  个人收入达前20%:61,665元

  家庭年收入中值:78,280元

  卡尔加里:451,609元

waters-3178900_640-600x399.jpg

  卡尔加里班芙公园。

  个人收入达前20%:78,924元

  家庭年收入中值:104,410元。

Thursday 15 March 2018

c# chart


add ado.net to fetch data from sql

change chart type in series property



using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApp2
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            using (testEntities1 _entity = new testEntities1())
            {
                List<spending> spending_list = new List<spending>();
                spending_list = _entity.spendings.ToList();

                dataGridView1.DataSource = spending_list;

                foreach(var item in spending_list)
                {
                    chart1.Series["spending"].Points.AddXY(item.category, item.percentage);
                }
            }
        }
    }
}



reference:
https://www.c-sharpcorner.com/article/crud-operation-using-entity-framework-in-windows-form-application/
https://www.c-sharpcorner.com/UploadFile/1e050f/chart-control-in-windows-form-application/
https://www.youtube.com/watch?v=6ua-IegyKB4

c# Fibonacci vortex



//form1.cs

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using ClassLibrary1;

namespace WindowsFormsApp1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Paint(object sender, PaintEventArgs e)
        {
            // Create points that define curve.
            var origin = new Point(400, 400);

            Point[] curvePoints = new Point[14];
            for (int i = 0; i < curvePoints.Length; i++)
            {
                curvePoints[i] = my_function.point_position(i + 1, 90 * i, origin);
            }

            Pen[] pens = new Pen[]
            {
                new Pen(Color.Red,2),
                new Pen(Color.Green,2),
                new Pen(Color.Blue,2),
                new Pen(Color.Gold,2)
            };

            GraphicsPath gp = new GraphicsPath();
            gp.AddCurve(curvePoints);

            for(int i=0;i<4;i++)
            {
                e.Graphics.DrawPath(pens[i], gp);
                gp = rotate_graphic(90, gp, origin);
            }

        }

        private GraphicsPath rotate_graphic(float angle, GraphicsPath g, Point center)
        {
            Matrix rmatrix = new Matrix(1, 0, 0, 1, 0, 0);
            rmatrix.RotateAt(angle, center);
            g.Transform(rmatrix);

            return g;
        }

    }
}

---------------------------------------------------------
//class library

using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ClassLibrary1
{
    public class my_function
    {
        public static int fibonacci(int n)
        {
            switch(n)
            {
                case 1:
                    return 1;
                case 2:
                    return 1;
                default:
                    return fibonacci(n - 2) + fibonacci(n - 1);
            }
        }

        public static Point point_position(int n, double angle, Point origin)
        {
            int radius = fibonacci(n);

            Point position = origin;
            position.X += Convert.ToInt32(radius * Math.Cos(Math.PI * angle / 180));
            position.Y += Convert.ToInt32(radius * Math.Sin(Math.PI * angle / 180));

            return position;
        }
    }
}

Sunday 11 March 2018

c# Task.Run async await

//run task one by one in sequence

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            my_timer = Stopwatch.StartNew();

            work_sequence();
         
            Console.Read();
        }

        private static void Work(int n)
        {
            Console.WriteLine("working on thread " + n + " at "+my_timer.Elapsed);
            Thread.Sleep(1000);
            Console.WriteLine("done thread " + n + " at " + my_timer.Elapsed);
        }     

        private static Stopwatch my_timer { get; set; }

        private async static void work_sequence()
        {
            for(int i=0;i<5;i++)
            {
                await Task.Run(() => Work(i+1));
            }

        }
    }
}

----------------------------------------------------
working on thread 1 at 00:00:00.0348544
done thread 1 at 00:00:01.0371825
working on thread 2 at 00:00:01.0379072
done thread 2 at 00:00:02.0383723
working on thread 3 at 00:00:02.0388026
done thread 3 at 00:00:03.0392966
working on thread 4 at 00:00:03.0401340
done thread 4 at 00:00:04.0410059
working on thread 5 at 00:00:04.0414497
done thread 5 at 00:00:05.0418085

c# thread synchronous join

//synchronous join
//all thread in first group starts simultaneously and processed synchronously 
//wait for all thread in first group to finish
//all thread in second group starts simultaneously and processed synchronously 
//wait for all thread in second group to finish

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace ConsoleApp1
{
    class Program
    {

        static void Main(string[] args)
        {
            var timer = Stopwatch.StartNew();

            Thread[] thread_array = new Thread[5];
            for (int i = 0; i < thread_array.Length; i++)
            {
                thread_array[i] = new Thread(new ThreadStart(work));
                thread_array[i].Name = "group 1 thread "+(i + 1).ToString();
                thread_array[i].Start();
            }

            for (int i = 0; i < thread_array.Length; i++)
            {
                thread_array[i].Join();
            }

            Console.WriteLine("done group 1 in " + timer.Elapsed);

            thread_array = new Thread[5];
            for (int i = 0; i < thread_array.Length; i++)
            {
                thread_array[i] = new Thread(new ThreadStart(work));
                thread_array[i].Name = "group 2 thread " + (i + 1).ToString();
                thread_array[i].Start();
            }

            for (int i = 0; i < thread_array.Length; i++)
            {
                thread_array[i].Join();
            }

            timer.Stop();
            Console.WriteLine("done all in " + timer.Elapsed);

            Console.Read();
        }

        private static void work()
        {
            var work_timer = Stopwatch.StartNew();
            Console.WriteLine("working on thread " + Thread.CurrentThread.Name);
            Thread.Sleep(1000);
            work_timer.Stop();
            Console.WriteLine("done thread " + Thread.CurrentThread.Name + " in " + work_timer.Elapsed);
        }
    }
}

---------------------------------------------------------

working on thread group 1 thread 1
working on thread group 1 thread 2
working on thread group 1 thread 3
working on thread group 1 thread 4
working on thread group 1 thread 5
done thread group 1 thread 2 in 00:00:01.0023935
done thread group 1 thread 1 in 00:00:01.0038165
done thread group 1 thread 3 in 00:00:01.0021814
done thread group 1 thread 4 in 00:00:01.0235408
done thread group 1 thread 5 in 00:00:01.0079507
done group 1 in 00:00:01.0527982
working on thread group 2 thread 1
working on thread group 2 thread 2
working on thread group 2 thread 3
working on thread group 2 thread 4
working on thread group 2 thread 5
done thread group 2 thread 1 in 00:00:01.0005360
done thread group 2 thread 2 in 00:00:01.0018397
done thread group 2 thread 3 in 00:00:01.0019317
done thread group 2 thread 4 in 00:00:01.0023157
done thread group 2 thread 5 in 00:00:01.0029776
done all in 00:00:02.1052437

---------------------------------------------------
//when threads are being processed, start new thread
//new thread starts before previous thread finish

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            my_timer = Stopwatch.StartNew();         

            Thread[] thread_array = new Thread[5];

            for(int i=0; i<thread_array.Length;i++)
            {
                thread_array[i] = new Thread(new ThreadStart(work));
                thread_array[i].Name = (i + 1).ToString();
                thread_array[i].Start();
                Thread.Sleep(1000);
            }

            for(int i=0;i<thread_array.Length;i++)
            {
                thread_array[i].Join();
            }

            my_timer.Stop();
            Console.WriteLine("done all in " + my_timer.Elapsed);

            Console.Read();
        }

        private static void work()
        {
            Console.WriteLine("working on thread " + Thread.CurrentThread.Name + " at "+my_timer.Elapsed);
            Thread.Sleep(3000);
            Console.WriteLine("done thread " + Thread.CurrentThread.Name + " at " + my_timer.Elapsed);
        }

        private static Stopwatch my_timer { get; set; }
    }
}

------------------------------------------------------

working on thread 1 at 00:00:00.0041648
working on thread 2 at 00:00:01.0049986
working on thread 3 at 00:00:02.0054949
done thread 1 at 00:00:03.0048240
working on thread 4 at 00:00:03.0079768
done thread 2 at 00:00:04.0060148
working on thread 5 at 00:00:04.0132561
done thread 3 at 00:00:05.0065144
done thread 4 at 00:00:06.0083626
done thread 5 at 00:00:07.0141073
done all in 00:00:07.0154234

------------------------------------------------------

Saturday 10 March 2018

C# Barrier

http://dotnetpattern.com/threading-barrier
https://docs.microsoft.com/en-us/dotnet/standard/parallel-programming/chaining-tasks-by-using-continuation-tasks

Linq – Action vs Func vs Predicate in C#

The purpose of writing this article is simple; to provide a simple and fresh demonstration of Action vs Func vs Predicate keyword in C#.
Though widely used with Linq, Action and Func are concepts logically independent of Linq. C++ already contained the basic concept in form of typed function pointers.
  • Action is a delegate (pointer) to a method, that takes zero, one or more input parameters, but does not return anything.
  • Func is a delegate (pointer) to a method, that takes zero, one or more input parameters, and returns a value (or reference).
  • Predicate is a function pointer for method which returns boolean value. They are commonly used for iterating a collection or to verify if the value does already exist.

Here is a small example for Action, Func and Predicate without using Linq